groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Winnebeck, Jason" <>
Subject RE: .with() variant that returns the original object
Date Tue, 08 Nov 2016 15:20:19 GMT
Normally, I'd say that we should use Java as first inspiration and all other popular JVM languages
(including Kotlin) as second inspiration, because it's really nice as a JVM-ecosystem developer
to have a common set of terminology and methods. However, I agree with Paul's analysis. The
meaning of "apply" conflicts with Java's Function.apply as well as apply from JavaScript world
(I don't know if others agree but I think of Groovy in a similar mindset to JS). The closest
Java signature is Function.identity, but identity is already used in Groovy and most think
of identity function as "doing nothing".

Therefore, my vote is for tap. When I see tap it makes sense to me. It makes me think of the
Unix "tee" shell command that does the same thing by sending the object to another command
and returning the same output unmodified. The usage of tap here is also consistent with network
terminology (a network tap), and real-world usage (such as "tapping" a water pipe). A tap
observes the input without changing it and allowing it to pass unimpeded, unlike a filter
or a function.


-----Original Message-----
From: Paul King [] 
Sent: Tuesday, November 08, 2016 9:34 AM
Subject: .with() variant that returns the original object

Hi everyone,

We are hoping to release 2.5 not too far down the track. We are working on a revamped release
process that is going to dramatically improve our ability to release early/release often but
also comply with some additional Apache requirements that we follow these days.
But more on that another time.

One outstanding feature request targeted for potential inclusion in
2.5 is an alternative to .with{} that automatically returns the original object after executing
the closure - recall that .with{} returns the last evaluated expression. The proposal is here:

We can't use the old name since that would break backward compatibility and is of utility
in its current form in any case, so we are looking for a new name for the proposed method.
If you look at the PR you will see it has been called 'tap' and 'doto' and numerous other
names have been suggested. We regard naming as very important and normally we'd have very
strong views about suitable names based on functionality and similar method names within the
Groovy codebase. But in this instance, an obvious name hasn't popped out at us, so we are
requesting feedback from the community about what names make most sense to you.

Firstly, here is what the method does. I'll use 'tap' in these examples since that is what
the PR currently uses but we can easily change that based on feedback.

myObject.tap {
    // some code

is equivalent to:

myObject.with {
    // some code
    return this

Returning the 'self' object lends itself to various kinds of chaining, e.g.

assert [:].tap {
    a = 1
}.tap {
    b = 2
} == [a:1, b:2]

Or this one (adapted from a blog post[1] - and assuming you have a file
containing answer=42 as one of the properties):

assert new Properties().tap {
    new FileInputStream('').withCloseable {
}.answer == '42'

Here are some of the names that have been suggested with some commentary:

doto    Used by Clojure. Not camel case as per normal convention
(though we have upto and downto which also break that convention) and it isn't immediately
obvious which is which between with and doto just from the names

tap    Comes from Ruby and a previous Groovy extension outside core
exists; meant to conjure up the idea of tapping into an object

autoWith    Same as with but automatically returns self

doWith   Again, hard to distinguish between doWith and with from the
names themselves

tee    An alternative name for tap

auto    A shortened version of 'autoWith'

apply    same as Kotlin which has copied Groovy's with but suffers
from the downside that apply is already heavily overleaded in other contexts, e.g. functional

withThis    Distinction with normal with a bit subtle perhaps

asThis    Ditto

within    Ditto

I'll also point out the 'identity' is currently an alias for 'with', in case that provides
any additional inspiration.

Perhaps you dis/like one of the above or have some other suggestions.
Let us know.

Cheers, Paul.


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.
View raw message