Re:Garbage collection with Automatic Resource Management in ...
by JavaFirehose on Monday January 16, 2012 @11:28AM
(#3471204)
It's a much needed cleanup of something that's always resulted in clutter in my code. C# has had this feature for over a decade. Nice to see Java adding this since I have a lot of Java code to maintain. Like a lot of Java devs we're switching over to Scala for new projects and using vanilla Java for legacy code. Automatic resource management can be implemented trivially in Scala without the need for a new language feature to support it. Structural subtyping is awesome!
[ Parent
| on HN ]
Re:Garbage collection with Automatic Resource Management in ...
by oconnor0 on Monday January 16, 2012 @11:44AM
(#3471314)
How does structural subtyping help you implement automatic resource management?
Re:Garbage collection with Automatic Resource Management in ...
by JavaFirehose on Monday January 16, 2012 @11:54AM
(#3471366)
I can define a resource management method which accepts any object which belongs to a class that provides a close method irrespective of whether that class implements close as part of an an interface or not.
Re:Garbage collection with Automatic Resource Management in ...
by grepgrep on Monday January 16, 2012 @12:16PM
(#3471481)
Rough example below, partially taken from a book I have on Scala.
//define it like this: def using[T <: {def close(): Unit }, S](obj: T) (operation: T => S) = { val result = operation(obj) obj.close() result }
//use it like this: val writer = new FunkyWriter using(writer) { writer.write("outputting some data") }
ARM was therefore possible in Scala before the Closable interface was even added to the Java libs.
Re:Garbage collection with Automatic Resource Management in ...
by sixcorners on Monday January 16, 2012 @12:29PM
(#3471535)
(I had another comment here but I figured out the answer.)
lombok's @Cleanup will close anything, interface or not.
Re:Garbage collection with Automatic Resource Management in ...
by grepgrep on Monday January 16, 2012 @12:35PM
(#3471562)
I took a look at lombok and it's pretty obvious from the way cleanup method names can be passed as strings that's it's using reflection. It's a bit of a nasty hack, but it works.
Re:Garbage collection with Automatic Resource Management in ...
by sixcorners on Monday January 16, 2012 @11:45PM
(#3473779)
It fiddles with the AST during compilation. No reflection.
Re:Garbage collection with Automatic Resource Management in ...
by thebluesky on Tuesday January 17, 2012 @01:23AM
(#3473984)
Sounds like it's trying to add structural subtyping via the back door.