Details
-
Type:
Improvement
-
Status:
Open
-
Priority:
Major
-
Resolution: Unresolved
-
Affects Version/s: 1.7.0
-
Fix Version/s: None
-
Component/s: syntax
-
Labels:None
-
Testcase included:yes
-
Number of attachments :
Description
In this code:
class Foo { String bar }
def x = new Foo().with {
bar = "baz"
}
"x" is now a String with the value "baz" thanks to the implied return. To work around this, we have to say:
def x = new Foo().with {
bar = "baz"
return delegate
}
...which is pretty ugly, to say the least.
So can we modify the "with" syntax to have an implied "return delegate"? I doubt anyone was depending on the previous behavior for code correctness.
Test will be this:
class Foo { String bar }
def x = new Foo().with {
bar = "baz"
}
assert x instanceof Foo
assert x.bar == "baz"
Issue Links
| This issue is duplicated by: | ||||
| GROOVY-4442 | 'with' method should return the object in which it was called |
|
|
|
you can have "it" as last statement in the closure too, to get what you want. The problem I see here is that the change you propose is a breaking one.