groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dmitry Semionin <>
Subject Re: Interpolated strings and embedded closures
Date Sun, 19 Jul 2015 12:47:44 GMT
Hi Jochen, and thanks for the reply.

On Sun, Jul 19, 2015 at 9:14 AM, Jochen Theodorou <> wrote:

> parameter = variable (with type) declared in a method/closure
> argument  = value (with type) used to call a method/closure

Yep, i totally confused the terms 'parameter' and 'argument' with each
other. Thanks for clearing it for me. But, apart from that, the question
still stays.

So it means the closure is called with a value of type StringWriter. The
> parameter can of course have any type that is a parent to StringWriter. No
> type being used means for example Object. But if you supply an incompatible
> type, you will get an exception on call. For example if you give the
> parameter w the type int, then you do have leftShift on there, but the
> method call to the closure itself is still done with a StringWriter and
> will fail.

Wait a minute. So am i getting it correct that all the embedded closures
that are declared to have a parameter take an implicit argument of the type
StringWriter by design? Because that code snippet i mentioned,

def  sOneParamClosure=  "1 + 2 == ${ w -> w << 3}"
assert  sOneParamClosure==  '1 + 2 == 3'

is self-sufficient in the documentation, and these two statements give no
reason to assume that there's some variable around named 'w' of type
StringWriter that was simply bound by the closure. Plus, it would be
illegal syntax-wise because 'w' in the first statement is the name of the
argument, which means it has a local visibility scope and refers to an
argument passed to the closure. Am i wrong here?

So either all the parameterized embedded closures do by design take an
implicit argument of type StringWriter, or my question remains: how does
one pass arguments to such closures? I mean, let's view it as the manual
tells us. Here - - it says the

The placeholder expressions are surrounded by ${} or prefixed with $ for
dotted expressions. The expression value inside the placeholder is
evaluated to its string representation when the GString is passed to a
method taking a String as argument by calling toString() on that expression.

If the embedded closures are a special case of string interpolation and
interpolated strings are evaluated upon conversion to regular Java strings,
then the second statement from the code block above is a place where such
conversion takes place. So if one can somehow pass an argument to the
embedded closure, it should be either here or somewhere above. But i don't
see anything that might count for it.

So what am i getting wrong?

- Dmitry

View raw message