From dev-return-7211-archive-asf-public=cust-asf.ponee.io@groovy.apache.org Wed Dec 2 22:33:52 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mxout1-he-de.apache.org (mxout1-he-de.apache.org [95.216.194.37]) by mx-eu-01.ponee.io (Postfix) with ESMTPS id 5470F180643 for ; Wed, 2 Dec 2020 23:33:52 +0100 (CET) Received: from mail.apache.org (mailroute1-lw-us.apache.org [207.244.88.153]) by mxout1-he-de.apache.org (ASF Mail Server at mxout1-he-de.apache.org) with SMTP id 9492D67575 for ; Wed, 2 Dec 2020 22:33:51 +0000 (UTC) Received: (qmail 75502 invoked by uid 500); 2 Dec 2020 22:33:50 -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 75486 invoked by uid 99); 2 Dec 2020 22:33:50 -0000 Received: from spamproc1-he-de.apache.org (HELO spamproc1-he-de.apache.org) (116.203.196.100) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Dec 2020 22:33:50 +0000 Received: from localhost (localhost [127.0.0.1]) by spamproc1-he-de.apache.org (ASF Mail Server at spamproc1-he-de.apache.org) with ESMTP id 6EB821FF39C for ; Wed, 2 Dec 2020 22:33:49 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamproc1-he-de.apache.org X-Spam-Flag: NO X-Spam-Score: -3.064 X-Spam-Level: X-Spam-Status: No, score=-3.064 tagged_above=-999 required=6.31 tests=[HTML_MESSAGE=0.2, KAM_DMARC_STATUS=0.01, NICE_REPLY_A=-3.576, POISEN_SPAM_PILL=0.1, POISEN_SPAM_PILL_1=0.1, POISEN_SPAM_PILL_3=0.1, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-he-de.apache.org ([116.203.227.195]) by localhost (spamproc1-he-de.apache.org [116.203.196.100]) (amavisd-new, port 10024) with ESMTP id X7nVauj3XJTm for ; Wed, 2 Dec 2020 22:33:48 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=81.19.149.134; helo=mx24lb.world4you.com; envelope-from=mgbiz@arscreat.com; receiver= Received: from mx24lb.world4you.com (mx24lb.world4you.com [81.19.149.134]) by mx1-he-de.apache.org (ASF Mail Server at mx1-he-de.apache.org) with ESMTPS id 632777F863 for ; Wed, 2 Dec 2020 22:33:48 +0000 (UTC) Received: from [217.149.171.18] (helo=[10.0.0.1]) by mx24lb.world4you.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94) (envelope-from ) id 1kkagv-0000P2-11; Wed, 02 Dec 2020 23:33:41 +0100 Subject: Re: Local variable declaration enhancements -- statement scoping To: dev@groovy.apache.org, OCsite References: From: MG Message-ID: <2b18e59c-8a25-041b-5916-0ae7d5955bb5@arscreat.com> Date: Wed, 2 Dec 2020 23:33:42 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------082B54819304D7D2713AB2F1" Content-Language: en-US X-AV-Do-Run: Yes This is a multi-part message in MIME format. --------------082B54819304D7D2713AB2F1 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Hi OC, I think that generally speaking, hiding/masking an outer variable like that is a quite undesireable coding style, so I like the current Groovy behavior (even if it deviates from C, evidently - I never used code like that in C, so I did not even know it was valid ;-) ). What specific use case did you have in mind, where just renaming the inner variable to i0, j, k, ... or the outer to index, idx, ... would not be the better solution ? (I use an informal coding style where I use variable names with a number at the end for short term / loop / etc variables, and for parameters and variables who live throughout a method or larger block I use no number postfix or longer names; the short name / long name meta at least is quite common, I think) Cheers, mg On 02/12/2020 18:13, OCsite wrote: > Hello there, > > when touching this stuff, it would be extremely desirable primarily to > fix the scoping/obscuring of same-named variables, which Groovy at the > moment does wrong, same as the demented Java thing: > > === > 89 ocs*/tmp>* def i=0 // outer > println "i=$i (outer)" > for (int i=1 /* inner */;i<2;i++) println "i=$i (inner)" > println "i=$i (outer again)" > 89 ocs*/tmp>* /usr/local/groovy-4.0.0-alpha-1/bin/groovy q > org.codehaus.groovy.control.MultipleCompilationErrorsException: > startup failed: > /private/tmp/q.groovy: 3: The current scope already contains a > variable of the name i > �@ line 3, column 10. > for (int i=1 /* inner */;i<2;i++) println "i=$i (inner)" > � � � � � ^ > 1 error > 90 ocs*/tmp>* > === > > This is how it *should* work: > > === > 90 ocs*/tmp>* #include > int main() { > int i=0; > printf("i=%d (outer)\n",i); > for (int i=1 /* inner */;i<2;i++) printf("i=%d (inner)\n",i); > printf("i=%d (outer again)\n",i); > return 0; > } > 91 ocs*/tmp>* cc -Wall q.c && ./a.out > i=0 (outer) > i=1 (inner) > i=0 (outer again) > 92 ocs*/tmp>* > === > > Thanks and all the best, > OC > >> On 2 Dec 2020, at 17:34, Milles, Eric (TR Technology) >> > > wrote: >> >> Traditional "for" (first example) and ARM "try" (last example) >> support local variable declarations that are scoped to the >> statement.� In light of the upcoming "instanceof" enhancement in >> Java, I was thinking about possible alternatives for declaring local >> variables that have statement scope. >> for (int i = ...; ...) { >> � // i available >> } >> // i unavailable >> for (x in y index i) { // from Gosu >> (http://gosu-lang.github.io/docs.html >> ) -- an alternative to using >> eachWithIndex >> } >> if (x instanceof T t) { // from Java 14+ >> } >> if (def x = ...) { // tests Groovy truth in this form; may be wrapped >> in parens to check something else about "x" >> } >> try (def ac = ...) { >> } >> This e-mail is for the sole use of the intended recipient and >> contains information that may be privileged and/or confidential. If >> you are not an intended recipient, please notify the sender by return >> e-mail and delete this e-mail and any attachments. Certain required >> legal entity disclosures can be accessed on our >> website:https://www.thomsonreuters.com/en/resources/disclosures.html >> > --------------082B54819304D7D2713AB2F1 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: 8bit Hi OC,

I think that generally speaking, hiding/masking an outer variable like that is a quite undesireable coding style, so I like the current Groovy behavior (even if it deviates from C, evidently - I never used code like that in C, so I did not even know it was valid ;-) ).

What specific use case did you have in mind, where just renaming the inner variable to i0, j, k, ... or the outer to index, idx, ... would not be the better solution ?
(I use an informal coding style where I use variable names with a number at the end for short term / loop / etc variables, and for parameters and variables who live throughout a method or larger block I use no number postfix or longer names; the short name / long name meta at least is quite common, I think)

Cheers,
mg


On 02/12/2020 18:13, OCsite wrote:
Hello there,

when touching this stuff, it would be extremely desirable primarily to fix the scoping/obscuring of same-named variables, which Groovy at the moment does wrong, same as the demented Java thing:

===
89 ocs /tmp> <q.groovy
def i=0 // outer
println "i=$i (outer)"
for (int i=1 /* inner */;i<2;i++) println "i=$i (inner)"
println "i=$i (outer again)"
89 ocs /tmp> /usr/local/groovy-4.0.0-alpha-1/bin/groovy q
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/private/tmp/q.groovy: 3: The current scope already contains a variable of the name i
�@ line 3, column 10.
�� for (int i=1 /* inner */;i<2;i++) println "i=$i (inner)"
� � � � � � ^
1 error
90 ocs /tmp>
===

This is how it should work:

===
90 ocs /tmp> <q.c
#include <stdio.h>
int main() {
� int i=0;
� printf("i=%d (outer)\n",i);
� for (int i=1 /* inner */;i<2;i++) printf("i=%d (inner)\n",i);
� printf("i=%d (outer again)\n",i);
� return 0;
}
91 ocs /tmp> cc -Wall q.c && ./a.out� � � � � � � � � � �
i=0 (outer)
i=1 (inner)
i=0 (outer again)
92 ocs /tmp>
===

Thanks and all the best,
OC

On 2 Dec 2020, at 17:34, Milles, Eric (TR Technology) <eric.milles@thomsonreuters.com> wrote:

Traditional "for" (first example) and ARM "try" (last example) support local variable declarations that are scoped to the statement.� In light of the upcoming "instanceof" enhancement in Java, I was thinking about possible alternatives for declaring local variables that have statement scope.
for (int i = ...; ...) {
� // i available
}
// i unavailable
for (x in y index i) { // from Gosu (http://gosu-lang.github.io/docs.html) -- an alternative to using eachWithIndex
}
if (x instanceof T t) { // from Java 14+
}
if (def x = ...) { // tests Groovy truth in this form; may be wrapped in parens to check something else about "x"
}
try (def ac = ...) {
}
This e-mail is for the sole use of the intended recipient and contains information that may be privileged and/or confidential. If you are not an intended recipient, please notify the sender by return e-mail and delete this e-mail and any attachments. Certain required legal entity disclosures can be accessed on our website:https://www.thomsonreuters.com/en/resources/disclosures.html


--------------082B54819304D7D2713AB2F1--