From dev-return-5344-archive-asf-public=cust-asf.ponee.io@groovy.apache.org Sat Sep 8 06:03:11 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id F4032180672 for ; Sat, 8 Sep 2018 06:03:10 +0200 (CEST) Received: (qmail 53990 invoked by uid 500); 8 Sep 2018 04:03:09 -0000 Mailing-List: contact dev-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list dev@groovy.apache.org Received: (qmail 53980 invoked by uid 99); 8 Sep 2018 04:03:09 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 08 Sep 2018 04:03:09 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id C2123CC345 for ; Sat, 8 Sep 2018 04:03:08 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 4.552 X-Spam-Level: **** X-Spam-Status: No, score=4.552 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, HTML_MESSAGE=2, KAM_INFOUSMEBIZ=0.75, KAM_NUMSUBJECT=0.5, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, T_DKIMWL_WL_MED=-0.01, URI_HEX=1.313] autolearn=disabled Authentication-Results: spamd1-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=asert-com-au.20150623.gappssmtp.com Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id FUETEnKi3GKn for ; Sat, 8 Sep 2018 04:03:07 +0000 (UTC) Received: from mail-ed1-f67.google.com (mail-ed1-f67.google.com [209.85.208.67]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTPS id 32DF45F1F0 for ; Sat, 8 Sep 2018 04:03:07 +0000 (UTC) Received: by mail-ed1-f67.google.com with SMTP id a20-v6so12846844edd.4 for ; Fri, 07 Sep 2018 21:03:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=asert-com-au.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:reply-to:from:date:message-id :subject:to; bh=94TTu/YZD0WGiC/lI0t9xNNC2Gj73XxoVeKFGQuncxo=; b=B65oARlmziZSaFxqiy8rjNrC/PF6ERB+nfuKK9noKW2e0tquyWSh4yuzQrUmWSNwB/ LEf8tenaGLpcFVnrdHNXWuiVTTOQBG9JoKPPLK/M7ybW70V9nZbuBR80w533ui84etl/ bjYhBfJWeP3TrP4BzNXKkyd7rtRF693XIGoxa+SE76FHI+KAsBHh9JOoizwm9KEqPE9z AaFHF8kwMx9UOqFoiDzfb0s73xm3SSGHZrc8gm2VIoNfNZs4mTHOsoxdoE8TEL6I8t6C 0zHRF67MPkrEBBaC4428r2P+Iugp/6N4BjDjmF84S2nMuMnmjeQUNE3qgVPVHmx2lut3 T0qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:reply-to :from:date:message-id:subject:to; bh=94TTu/YZD0WGiC/lI0t9xNNC2Gj73XxoVeKFGQuncxo=; b=WG06QhT/1dj3TA8yoqeX+zectQJUvIwmduI/ee68qULiIbHiFt1ecsClX/NDwJmCmY NqM0G0pfmUm1/zf9uZW9JaCSVPLSNq0Nb7cTNOpyiZDqe4sNanKmg0ZqChRAR3T0wjS3 AvmYdzKkYC4yWnNbD/Vddrmy2DKkjC/WULHs48gadfWoi7I6KqQvmnyg9gJUsMzD18Fn fSXb/J3i9BtX2wR6Ew7Esd0PvPSPQS5b/lx/L7C7HkzP3kYZdB4B+albNLRjY5WW1ZBb uNkwDxApIyNyAfRQP2tN6d+sWVsSW6SGFuo1xOaRDvdDzqDM8yu2dfK0X2vSAZ0qIlzo Dq5Q== X-Gm-Message-State: APzg51A9TdLMOL4m/8YM7I3on4S4lS9n6IYRabwb1SvbUgtXdvzuci9p RO5IYh7IQ8SNnYbLuB5hQ0JvnXS44Rvweqc6JEuoBBa+ X-Google-Smtp-Source: ANB0VdZrKggKpHpekxyFRDvTBvlo023gSs5RgK6ITLCjO6uFt3ZwhdRPnJEz3P2hQEBLeCHXUWa1TqzVoIsVdfkzZR8= X-Received: by 2002:a50:bf0d:: with SMTP id f13-v6mr11838006edk.157.1536379385803; Fri, 07 Sep 2018 21:03:05 -0700 (PDT) MIME-Version: 1.0 References: <1536368845803-0.post@n5.nabble.com> In-Reply-To: <1536368845803-0.post@n5.nabble.com> Reply-To: paulk@asert.com.au From: Paul King Date: Sat, 8 Sep 2018 14:02:56 +1000 Message-ID: Subject: Re: [Proposal] GString is implemented eager and treated as normal String since groovy 3.0.0 To: dev@groovy.apache.org Content-Type: multipart/alternative; boundary="0000000000008806bd05755433ef" --0000000000008806bd05755433ef Content-Type: text/plain; charset="UTF-8" I think a GEP would be the right thing for a change like this. I'd be willing to help try to put it together. I'd hope we could come up with an approach which allows eager or lazy. I don't want to get hung up on syntax but perhaps "${x}" could be eager and "${ -> x }" could be the current semantics or some alternative to this. But anyway, a GEP could outline the exact proposed change across dynamic and static Groovy with pros and cons etc. Cheers, Paul. On Sat, Sep 8, 2018 at 11:07 AM Daniel.Sun wrote: > Hi all, > > In most of cases, we just use GString as an enhanced String, but > currently GString is not implemented eager so it has some weird features, > which will confuse users. Let's look at some examples[1] from Jochen: > > * the referenced variables are bound eager > * toString on GString evaluates the GString, this includes toString for the > values > ``` > def x = 1 > def gstring = "$x" > assert gstring == "1" > x = 2 > assert gstring == "1" > ``` > ``` > class X { > int i = 0 > String toString(){Integer.toString(i++)} > } > def x = new X() > def gstring = "$x" > assert gstring == "0" > assert gstring == "1" // the content of gstring changed... > ``` > > If we implement GString eager and treated as normal String(not expose > GString API), the content of GString will not change once it is evaluated. > We can get the following benefits: > 1) Users will not be confused because of some weird feature; > 2) Better compatibility between dynamic and static compilation, SEE > GROOVY-6668[1]; > 3) The performance will be improved to some extent because GString will > not > be evaluated again and again even if its content is not changed; > > The proposal is a breaking change, so I propose it is targeted to > groovy 3.0.0 if accepted. > > P.S. some issues like GROOVY-6668 are raised by users again and again, that > is say, many users are confused by current implementation, i.e. GString can > not be treated as normal String in STC. In the static compilation mode: > ``` > @groovy.transform.CompileStatic > class Test { > static void main(String[] args) { > def m = ['a':1, 'b':2] > def k = 'a' > assert 1 == m[k] > assert 1 == m["$k" as String] > assert 1 == m["$k"] // fails, gets null, which is not equal to 1 > } > } > > ``` > > > Cheers, > Daniel.Sun > > [1] https://github.com/apache/groovy/pull/708 > > > > > > ----- > Daniel Sun > Apache Groovy committer > Blog: http://blog.sunlan.me > Twitter: @daniel_sun > > -- > Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html > --0000000000008806bd05755433ef Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I think a GEP would be the right thing for a change like t= his. I'd be willing to help try to put it together.
I'd hope we= could come up with an approach which allows eager or lazy.
I don= 't want to get hung up on syntax but perhaps "${x}" could be = eager and "${ -> x }"
could be the current semantics= or some alternative to this.

But anyway, a GEP co= uld outline the exact proposed change across dynamic
and static G= roovy with pros and cons etc.

Cheers, Paul.
<= div>

On Sat,= Sep 8, 2018 at 11:07 AM Daniel.Sun <sunlan@apache.org> wrote:
= Hi all,

=C2=A0 =C2=A0 =C2=A0 In most of cases, we just use GString as an enhanced S= tring, but
currently GString is not implemented eager so it has some weird features, which will confuse users. Let's look at some examples[1] from Jochen:
* the referenced variables are bound eager
* toString on GString evaluates the GString, this includes toString for the=
values
```
def x =3D 1
def gstring =3D "$x"
assert gstring =3D=3D "1"
x =3D 2
assert gstring =3D=3D "1"
```
```
class X {
=C2=A0 int i =3D 0
=C2=A0 String toString(){Integer.toString(i++)}
}
def x =3D new X()
def gstring =3D "$x"
assert gstring =3D=3D "0"
assert gstring =3D=3D "1" // the content of gstring changed... ```

=C2=A0 =C2=A0 =C2=A0 If we implement GString eager and treated as normal St= ring(not expose
GString API), the content of GString will not change once it is evaluated.<= br> We can get the following benefits:
1)=C2=A0 Users will not be confused because of some weird feature;
2)=C2=A0 Better compatibility between dynamic and static compilation, SEE GROOVY-6668[1];
3)=C2=A0 The performance will be improved to some extent because GString wi= ll not
be evaluated again and again even if its content is not changed;

=C2=A0 =C2=A0 =C2=A0The proposal is a breaking change, so I propose it is t= argeted to
groovy 3.0.0 if accepted.

P.S. some issues like GROOVY-6668 are raised by users again and again, that=
is say, many users are confused by current implementation, i.e. GString can=
not be treated as normal String in STC. In the static compilation mode:
```
@groovy.transform.CompileStatic
class Test {
=C2=A0 =C2=A0 static void main(String[] args) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 def m =3D ['a':1, 'b':2]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 def k =3D 'a'
=C2=A0 =C2=A0 =C2=A0 =C2=A0 assert 1 =3D=3D m[k]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 assert 1 =3D=3D m["$k" as String]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 assert 1 =3D=3D m["$k"] // fails, get= s null, which is not equal to 1
=C2=A0 =C2=A0 }
}

```


Cheers,
Daniel.Sun

[1] https://github.com/apache/groovy/pull/708





-----
Daniel Sun
Apache Groovy committer
Blog: http://blog.sunlan.me
Twitter: @daniel_sun

--
Sent from: http://groovy.329449.n5.nabble.= com/Groovy-Dev-f372993.html
--0000000000008806bd05755433ef--