From dev-return-5354-archive-asf-public=cust-asf.ponee.io@groovy.apache.org Sat Sep 8 18:05:37 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 E088A180674 for ; Sat, 8 Sep 2018 18:05:36 +0200 (CEST) Received: (qmail 52669 invoked by uid 500); 8 Sep 2018 16:05:35 -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 52644 invoked by uid 99); 8 Sep 2018 16:05:35 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 08 Sep 2018 16:05:35 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id C9632C035A; Sat, 8 Sep 2018 16:05:34 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.862 X-Spam-Level: * X-Spam-Status: No, score=1.862 tagged_above=-999 required=6.31 tests=[KAM_INFOUSMEBIZ=0.75, KAM_NUMSUBJECT=0.5, RCVD_IN_DNSWL_LOW=-0.7, SPF_PASS=-0.001, URI_HEX=1.313] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id A5Xs-OaycduJ; Sat, 8 Sep 2018 16:05:32 +0000 (UTC) Received: from mx16lb.world4you.com (mx16lb.world4you.com [81.19.149.126]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTPS id 24EF65F382; Sat, 8 Sep 2018 16:05:32 +0000 (UTC) Received: from [62.178.216.41] (helo=[192.168.0.28]) by mx16lb.world4you.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1fyfjh-0003L4-FR; Sat, 08 Sep 2018 18:05:25 +0200 Subject: Re: [Proposal] GString is implemented eager and treated as normal String since groovy 3.0.0 To: dev@groovy.apache.org, "Daniel.Sun" , dev@groovy.incubator.apache.org References: <1536368845803-0.post@n5.nabble.com> From: MG Message-ID: <1c1fd653-42e3-e9ee-62f6-0d78f4b2a3d8@arscreat.com> Date: Sat, 8 Sep 2018 18:05:24 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <1536368845803-0.post@n5.nabble.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-AV-Do-Run: Yes Hi Daniel, another flag from me: If I understand you correctly, you want to change existing GString literal semantics to make it work as if toString() had implicitely on it; i.e. there would be no more GString literal, but instead String interpolation support for regular String literals in double quotes ? This would be a framework breaking change for me, since I am using the GString feature to be able to access a GString's embedded objects extensively in my SQL building framework. Simple example: String category = "HEAD" final Table pe = Table.reference(PERSON) final Table org = Table.reference(ORG_TREE) GString sql = """     select $pe.ID, $pe.FIRST_NAME, $pe.LAST_NAME, $org.DEPARTMENT     from $pe, $org     where         $pe.ORG_ID = $org.ID and $org.CATEGORY = ${BindValue.val(category)} """ sql = tableToViewSubstitution(sql) // Replace some table instances with their View equivalent sql = renumberReferenceNames(sql) // Iterates over sql.objects, modifying embedded Table/Column/View/... objects from my framework SqlExecutor.create(...).rows(sql) // Convert passed sql GString into Groovy Sql compatible GString How is the proposed change expected to work with existing Groovy Sql support for implicitly interpreting embedded objects as bind values (since this is too restrictive/inflexible for heavy use I have changed this from implicit to explicit support (BindValue class) in my framework). Cheers, mg On 08.09.2018 03:07, 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 >