P.S. Or, it would be quite sufficient to be able to get the list of the closeables inside of the try-with-resource. I mean something like
=== static List somethingSmart() { ... } ... def a=new Foo(),b=new Foo(),c=new Foo() try (a) { assert somethingSmart()==[a] } try (b,c) { assert somethingSmart()==[b,c] } === Does Java or Groovy allow to write such kind of “somethingSmart”? How? Thanks, OC > On 2. 6. 2020, at 2:28 AM, o...@ocs.cz wrote: > > Is there some trick which would, with a proper replacement of ???, ensure > that the following code: > > === > class Foo { > void ???() { println "Hi" } > void close() { println "Bye" } > } > ... > def foo=new Foo() > println "1" > try (foo) { println "2" } > println "3" > try (foo) { println "4" } > println "5" > === > > prints out 1,Hi,2,Bye,3,Hi,4,Bye,5? > > I'd rather like not to create a new foo object for each try block (which of > course would be sorta-solution); it would be _considerably_ more convenient > to reuse it -- if there was a way for that to know it enters the try block. > > Thanks! > OC >