groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Winnebeck, Jason" <Jason.Winneb...@windstream.com>
Subject RE: .with() variant that returns the original object
Date Wed, 09 Nov 2016 21:02:46 GMT
Regarding Jochen's 
Please don't change the existing behavior of with, that was mentioned once before, and it
breaks backwards compatibility. I use it extensively in DSL code, here is a fictional DSL
example that is similar in what I use it for:

def peopleWhere(Closure c) { people.findAll { it.with(c) } }

def younglings = peopleWhere { age < 35 }

In this example, I use with to avoid the extra boilerplate of cloning the closure and setting
resolve strategy, delegate, and calling it.

Jason

-----Original Message-----
From: Jochen Theodorou [mailto:blackdrag@gmx.org] 
Sent: Wednesday, November 09, 2016 1:45 PM
To: users@groovy.apache.org
Subject: Re: .with() variant that returns the original object

On 09.11.2016 14:56, Winnebeck, Jason wrote:
> My concern about "withThis" is that it implies that "this" is the parameter of the closure
and not the return. We have for example withReader, withWriter, withOutputStream, etc. Those
all imply that the parameter is the reader, the writer, the output stream. So in my mind,
withThis tells me nothing at all about the fact that the original object is returned. withThis
would not be consistent with the rest of Groovy.

I agree with this one.

> .with(returnThis:true) not only has runtime overhead, but keep in mind we are comparing
this to the current state today, which is .with { return this }, or .with { this } depending
on your style.

here I have to correct a bit though. Just want to avoid we start discussing the wrong thing...
And I just noticed Paul made the very same mistake in the original post already. Well, maybe
not too late yet

we are talking about

foo.with {
   return foo
}

or

foo.with {
  return it
}

not about something returning "this" at any point. "return this" would return neither the
open block, nor foo, it would be the enclosing class instance. No delegate has influence about
any explicit this ever.

Ah, and I did just see Jason noticed it in a later mail... well, maybe saying it two times
is better ;)

Anyway, that´s why I think withThis and with(returnThis:true) are not good variants.

Also it should be noted that we already have an alias for "with", which is "identity". I would
not want to have yet another one.

Frankly... I think we should change what it returns. It is unlikely somebody did depend on
with returning null.

bye Jochen

This email message and any attachments are for the sole use of the intended recipient(s).
Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the
intended recipient, please contact the sender by reply email and destroy all copies of the
original message and any attachments.
Mime
View raw message