3 ms·
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 si
by JavaFirehose 15y ago
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!
- oconnor0 15y agoHow does structural subtyping help you implement automatic resource management?
- JavaFirehose 15y agoI 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.
- grepgrep 15y agoRough 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.
- sixcorners 15y ago(I had another comment here but I figured out the answer.) lombok's @Cleanup will close anything, interface or not.
- grepgrep 15y agoI 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.
- sixcorners 15y agoIt fiddles with the AST during compilation. No reflection.
- thebluesky 15y agoSounds like it's trying to add structural subtyping via the back door.