Is there a way for an object to know it _enters_ a try-with-resource block?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Is there a way for an object to know it _enters_ a try-with-resource block?

OCsite
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

Reply | Threaded
Open this post in threaded view
|

Re: Is there a way for an object to know it _enters_ a try-with-resource block?

OCsite
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, [hidden email] 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
>

Reply | Threaded
Open this post in threaded view
|

RE: Is there a way for an object to know it _enters_ a try-with-resource block?

Winnebeck, Jason
I would not be too concerned about having to create a new object. I've heard that modern JVMs are really good at detecting temporary objects that don't escape a method and turning them into stack objects.

So I would do something like this (let's assume you really don't want to create new Foo):

class FooSession implements Closeable {
  private final Foo foo
  FooSession(Foo foo) { this.foo = foo; foo.start(); }
  void close() { foo.close() }
}

def a = new Foo()
try(new FooSession(a)) { println "1" }

There is actually precedence for this in other languages: in C++ with RAII concept one uses the "lock" object pattern to ensure unlocking... as a way to get a kind of forced "finally" concept.

However, this still doesn't force the user to use the session. If you want to do that, and you can modify foo, why don't you add a start method to Foo?

class Foo { //notice Foo is NOT implements Closeable
  Closeable start() { println "Hi"; new FooSession(this); }
  void close() { println "Bye" }
}

def x = new Foo()
try (x.start()) { println "1" }

Of course, you could also have the start method take a closure -- if you don't care about seeing try-with-resources directly, then you can make the same pattern:

x.start { println "1" }

Jason


Sensitivity: Internal

-----Original Message-----
From: [hidden email] <[hidden email]>
Sent: Monday, June 1, 2020 8:43 PM
To: [hidden email]
Subject: Re: Is there a way for an object to know it _enters_ a try-with-resource block?

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, [hidden email] 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
>