Re: Alternatives to try/finally code structure

2020-11-24 Thread Christopher Smith
This is substantially lower-level than is probably warranted, but what you're looking for could be implemented by pushing the values onto the stack immediately at the entry point. On Tue, Nov 24, 2020, 15:36 Milles, Eric (TR Technology) < eric.mil...@thomsonreuters.com> wrote: > > since you seeme

RE: Alternatives to try/finally code structure

2020-11-24 Thread Milles, Eric (TR Technology)
> since you seemed to say in the beginning that you did not want to create > objects (if I read this correctly), why creating AutoCloseable instances here > would not pose a problem ?) Best case scenario is that I get the same performance and memory footprint of the original code (included belo

Re: Alternatives to try/finally code structure

2020-11-24 Thread MG
Something along that line, yes :-) (Just wondering, since you seemed to say in the beginning that you did not want to create objects (if I read this correctly), why creating AutoCloseable instances here would not pose a problem ?) Cheers, mg On 23/11/2020 00:45, Milles, Eric (TR Technology) w

RE: Alternatives to try/finally code structure

2020-11-22 Thread Milles, Eric (TR Technology)
Or I could still call it "push" and it could look like this: try (field.push(value)) { ... } -- > Now imagine that try-catch-finally wrapper method was an inlined closure, and > it inlines the closure it receives, then you get a solution to your problem > with very little overhead. Thi

RE: Alternatives to try/finally code structure

2020-11-22 Thread Milles, Eric (TR Technology)
> Now imagine that try-catch-finally wrapper method was an inlined closure, and > it inlines the closure it receives, then you get a solution to your problem > with very little overhead. This may be in alignment with what you are saying. If I had a method "auto(oldValue, newValue)" that creats

Re: Alternatives to try/finally code structure

2020-11-22 Thread MG
I was primarily referring to what Jochen said about a mixin being a possible solution: "could be a mixin... but if your requirement is to avoid the creation of additional objects, then this will not work, as the Closure will be created new every time.". But I also looked at your code, and saw

RE: Alternatives to try/finally code structure

2020-11-22 Thread Milles, Eric (TR Technology)
Or an alternative where the method creates an AutoCloseable instance that fits into a try-with-resources: class Foo { def bar() { try (push(field1 = value); push(field2)) { baz() } } } "push" would create something that has read/write access to "field1". Assignment "field1 =

RE: Alternatives to try/finally code structure

2020-11-22 Thread Milles, Eric (TR Technology)
I'm not understanding how inlining a closure expression could help reduce the burden of saving state to a temporary variable and then restoring it after execution, unless there was some additional AST processing. macro method take 2: class Foo { def bar() { push { field1 = value, field2 -

Re: Alternatives to try/finally code structure

2020-11-22 Thread MG
If Groovy would support closure-block constructs ("inlining closures"), this could be overcome without having to resort to writing one's own macro (which is more effort, especially if you do not already have a seperate macro module set up in your project). I still think inlining closures in cas

RE: Alternatives to try/finally code structure

2020-11-22 Thread Milles, Eric (TR Technology)
Yes, snapshot and rollback. Here is a concrete example of the pattern: https://github.com/groovy/groovy-eclipse/blob/master/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java#L430

Re: Alternatives to try/finally code structure

2020-11-22 Thread Jochen Theodorou
On 21.11.20 20:29, Milles, Eric (TR Technology) wrote: [...] I was thinking of something like this so as not to overload the try keyword: class Foo {   def bar() {     push (field1 = value, field2) { // AST would be re-written to try/finally like Scenario 1/2   baz()     }   }  

Re: Alternatives to try/finally code structure

2020-11-21 Thread Christopher Smith
ide the bar method's scope. > > > > My goal is to eliminate repetitious typing of assigning the current value to > a temporary variable, assigning the next value to the field and at the end > restoring the previous value. > > > > > > From: Leo Gertsenshtey

Re: Alternatives to try/finally code structure

2020-11-21 Thread MG
ary variable, assigning the next value to the field and at the end restoring the previous value. *From:* Leo Gertsenshteyn *Sent:* Saturday, November 21, 2020 5:34 PM *To:* dev@groovy.apache.org *Subject:* Re: Alternatives to try/finally code structure Hi Eric, Am I correctly interpreting

RE: Alternatives to try/finally code structure

2020-11-21 Thread Milles, Eric (TR Technology)
nshteyn Sent: Saturday, November 21, 2020 5:34 PM To: dev@groovy.apache.org Subject: Re: Alternatives to try/finally code structure Hi Eric, Am I correctly interpreting that baz() in the above examples calls into 3rd party code and that code has direct reference to the state and controls the state v

Re: Alternatives to try/finally code structure

2020-11-21 Thread Leo Gertsenshteyn
Hi Eric, Am I correctly interpreting that baz() in the above examples calls into 3rd party code and that code has direct reference to the state and controls the state variables' lifecycle? If that's not the case, there may much much simpler solutions involving intermediate objects, recursion, etc.

Alternatives to try/finally code structure

2020-11-21 Thread Milles, Eric (TR Technology)
Groovy Devs, I have been pondering how to automate the writing of try/finally blocks used to unconditionally restore object state. Does anyone know of a Groovier way to do something like this before I pursue a macro method, an AST transformation, or something more involved? It currently requi