groovy

Improve "with" by ending with implied "return delegate"

Details

  • Type: Improvement Improvement
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: 1.7.0
  • Fix Version/s: None
  • Component/s: syntax
  • Labels:
    None
  • Testcase included:
    yes
  • Number of attachments :
    0

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

Activity

Hide
blackdrag blackdrag added a comment -

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.

Show
blackdrag blackdrag added a comment - 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.
Hide
Robert Fischer added a comment -

Do you think there is someone who coded to the previous implementation?

Minimally, can we queue it up for 1.8 or 2.0?

Show
Robert Fischer added a comment - Do you think there is someone who coded to the previous implementation? Minimally, can we queue it up for 1.8 or 2.0?
Hide
Paul King added a comment -

Do you have a better example?
The one provided doesn't seem satisfying justification to me because of the obvious alternate syntax:

def x = new Foo(bar:'baz')
Show
Paul King added a comment - Do you have a better example? The one provided doesn't seem satisfying justification to me because of the obvious alternate syntax:
def x = new Foo(bar:'baz')
Hide
Robert Fischer added a comment -

It was simplified to illustrate the request.

Something along these lines is what raised the issue in the first place:

def x = new Foo().with {
  bars = []
  inputFile.withReader { reader ->
     def line
     while(line = reader.readLine() =~ /someCheck/) {
       bars << line
     }
  }
}

The variable "x" is now null (IIRC).

It was also just surprising to try out this code:

def x = new Foo().with {
  bar = "baz"
}

And discover that x is now "baz". Is that behavior supposed to be intuitive?

Show
Robert Fischer added a comment - It was simplified to illustrate the request. Something along these lines is what raised the issue in the first place:
def x = new Foo().with {
  bars = []
  inputFile.withReader { reader ->
     def line
     while(line = reader.readLine() =~ /someCheck/) {
       bars << line
     }
  }
}
The variable "x" is now null (IIRC). It was also just surprising to try out this code:
def x = new Foo().with {
  bar = "baz"
}
And discover that x is now "baz". Is that behavior supposed to be intuitive?

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated: