#1041 new method for OutStream

Akcelisto Fri 26 Mar 2010

There is slightly annoyed to write every time close.

out := `temp`.toFile.out
out.print(
  """using tests
     class B:A{}""")
out.close

New method arm:

out := `temp`.toFile.out.arm{
  print(
  """using tests
     class B:A{}""")
}

In OutSream.fan:

Bool arm(|This| f){
  try
    f(this)      
  finally
    return close
}

tactics Fri 26 Mar 2010

Something like this might be useful. However, it's quite as bad in Fantom as other language, since all write and print methods for streams return this. So, you can chain calls together:

File(`temp`).out.print("Hello world").write('\r').write('\n').close

katox Fri 26 Mar 2010

Though chaining close is not a good idea - it should stay in the finally block.

brian Fri 26 Mar 2010

The current convention is to call that method use (for example on Locale).

There was debate previously about C# style using clause that would automatically close one or more objects versus using a method like use with an it-block.

Akcelisto Fri 26 Mar 2010

"Using clause" is unnecessary complication. "Extension methods" can take the place "Using clause" when Brian will add them.

Bool use(this Closeable res, |This| f){
  try
    f(res)      
  finally
    return res.close
}

I think, now may be just add use in OutStream. Later, will be to remove use from OutStream when "Extension methods" appears.

Login or Signup to reply.