Return-Path: X-Original-To: apmail-groovy-users-archive@minotaur.apache.org Delivered-To: apmail-groovy-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7079517339 for ; Sun, 19 Jul 2015 13:27:17 +0000 (UTC) Received: (qmail 32989 invoked by uid 500); 19 Jul 2015 13:27:17 -0000 Delivered-To: apmail-groovy-users-archive@groovy.apache.org Received: (qmail 32948 invoked by uid 500); 19 Jul 2015 13:27:17 -0000 Mailing-List: contact users-help@groovy.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@groovy.incubator.apache.org Delivered-To: mailing list users@groovy.incubator.apache.org Received: (qmail 32938 invoked by uid 99); 19 Jul 2015 13:27:17 -0000 Received: from Unknown (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 19 Jul 2015 13:27:17 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id C163F1A739A for ; Sun, 19 Jul 2015 13:27:16 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1 X-Spam-Level: * X-Spam-Status: No, score=1 tagged_above=-999 required=6.31 tests=[KAM_LIVE=1, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-us-east.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id rPnLVAPfixiJ for ; Sun, 19 Jul 2015 13:27:07 +0000 (UTC) Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) by mx1-us-east.apache.org (ASF Mail Server at mx1-us-east.apache.org) with ESMTPS id 90E9343DA9 for ; Sun, 19 Jul 2015 13:27:06 +0000 (UTC) Received: from [192.168.1.3] ([85.180.110.5]) by mail.gmx.com (mrgmx102) with ESMTPSA (Nemesis) id 0ME47n-1ZATl13KbH-00HRZh for ; Sun, 19 Jul 2015 15:27:05 +0200 Message-ID: <55ABA5F6.7000006@gmx.org> Date: Sun, 19 Jul 2015 15:28:22 +0200 From: Jochen Theodorou User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: users@groovy.incubator.apache.org Subject: Re: Interpolated strings and embedded closures References: <55AB404D.7080606@gmx.org> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K0:NPjpphKkKVzNBMpp6KyJ+fJq+9yBjYbUBwtYKlSq8W5JpB2jCxh oYxQFzEQ4KG/eZwY9BaPjvkIwVrupklPsDJ5Qs6d4ja8hIDAvUn4smTTm5a9/2DlpLB7r6s vXbMF4aSqfDoXuKE9R2RjVSp6twaVAXhMZUg0i7SbqNX/93Hme5yjNL9fRC/T3FKN9wEgm5 UOBaEkWWb6gAyti7yDKrg== X-UI-Out-Filterresults: notjunk:1;V01:K0:NIaQbOiMjLY=:1qGwnXUUcVlsJS+loz0i0d j/qPVoSxpqWzxIAD2Cqb5083EL+6wNEBwPj4P5mcK/zw9PAAI9qAtLfLA+5Nf6h2nxtbP9ftb w0EUwYRX1+iGl848+XmnTC+GaUqu8TmK743JN62+7VpWGDlAAP+iQrWYuQp7My7aduzGiwl5a LzH3gnbg/AFZemOwqnI9SHRcY1MgKnSB9yaGuhLhEXfdIsp2GhQSKGvP6DyulxQvOH4NYCnjC 3QVjbUxkg0BHA9KmKvW/KjBcxEDr31pzShWHJlqrb+hfrQ+bvVyzuPDR6rE1QUL1Uu9FVFZJH JipcIZdUFBwMU5gY8Y6X1HunN9wCoPeRlioJEDqBeo61nca+aRAcZWN7Tg1VbkM8ozxoJUUyD gTqUM7MHdWuhaTNmTpUtfMmOKsIjwwD4cRWeDKTrwNebD2GNYCVPQ6SkUC6AQgmvVZsINekgC apmxEfXnQ/K85pnW3+5mU/Dx1deRRNoSYrKJ58UkN0RNyYuFLVDgmyfxmz3ytWxKvXqMSgLNw 7tlSO6PP4kD+qUa0mLjxO1YfB0Fa/wtY0JVSIM6PWAWgfl19PZ1eRvkR6J1HVTdeuHtwyeEM9 zAn5KhrQFQNj/QPRkiwagCBDFH/1JFS8k4GZVhJK9onBmCq//62B49/G+/zhX+wA25MkL3nON QE+c9WcSM0ZPhKy/MpiiQ9EGg4g0dRO66NkL+3ukI+Pgy7zBSxDs68fL3QvcIe6KuWAc= Am 19.07.2015 14:47, schrieb Dmitry Semionin: [...] > 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? { w -> } means you declare a "closure" with a parameter of name w. w is not of type StringWriter, but the closure will be called with a value of type StringWriter, and that will be accessed through w inside the "closure". Just imagine you would write this method: def call(w) { w<<3 } same deal. In Groovy you don't need always to give a type, which means Object will be used as the minimally required type. In other words the method declaration above expands to: def call(Object w) { w<<3} And def is an alias for Object so: Object call(Object w) {w<<3} Similar for {w->w<<3} becoming {Object w->w<<3} 'w' is the name of the parameter, with local visibility scope and allows access to the argument of the call. > 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? Again you wildly mix arguments and parameters ;) In the example you showed, there is no implicit parameter. {w->} is a closure with the explicit parameter w. {->} is a closure without parameter > I mean, let's view it as the manual > tells us. Here - > http://www.groovy-lang.org/syntax.html#_string_interpolation - it says > the following: > > 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. yes, but... "${x}" is a GString that won't contain a closure, while "${w->w< 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. A GString "foo $bar" (longer form "foo ${bar}"), is a GStringImpl object in Groovy, implementing the interface GString and basically consists of the String "foo " and a reference to the value of bar. If you call toString() on this GString it will execute "foo "+bar.toString() using a StringWriter. Meaning each toString call, will cause a new evaluation. A GString "foo ${w->w<w<