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 requires a lot of typing to do this.
Scenario 1: stack-ify a scalar field/property -- often used when traversing a
list or tree and "state" is the current element
class Foo {
private state
def bar() {
def temp = state // may be any number of fields saved to temp vars
state = newState
try {
baz()
} finally {
state = temp
}
}
def baz() {
// make use of state; does not require previous values
}
}
Scenario 2: mutation rollback -- similar but "state" is not written to
beforehand
class Foo {
private state
def bar() {
def temp = state // may be any number of fields saved to temp vars
try {
baz()
} finally {
state = temp
}
}
def baz() {
// modifies state
}
}
Note: "state" is not always convertible into a java.util.Stack. Sometimes this
is 3rd-party code that is being extended.
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()
}
}
...
}
This e-mail is for the sole use of the intended recipient and contains
information that may be privileged and/or confidential. If you are not an
intended recipient, please notify the sender by return e-mail and delete this
e-mail and any attachments. Certain required legal entity disclosures can be
accessed on our website:
https://www.thomsonreuters.com/en/resources/disclosures.html