[ 
https://issues.apache.org/jira/browse/GROOVY-11525?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17904493#comment-17904493
 ] 

Jochen Theodorou commented on GROOVY-11525:
-------------------------------------------

[~emilles]
I initially tried to compare it with the for-loop, but the for-loop is a 
different construct that only looks similar. In case of the for-loop you have 
the init, compare and aggregation parts, surrounded by () and divided by ;. The 
execution is basically lazy, as part of that evaluated over and over again, but 
also a bit  eager, since some parts can be seen as executed right away. 
The  another interesting compare is probably the while-loop:
{code:Java}
char c
while((c = stream.peek()) != ' ') {
...
}
{code} could become
{code:Java}
while((char c; (c = stream.peek()) != ' ') ) {
....
}
{code} And now compare back with the for-loop:
{code:Java}
for(char c; (c = stream.peek()) != ' '); ) {
....
}
{code} Of course with the very big difference, that the c is not accessible in 
the block in the while-case.

[~daniel_sun]
So far I do like the general idea, but I do not like the syntax so much. For my 
taste it is too near to other constructs, while having quite different 
semantics and not so much syntactic difference. I am missing the "key 
indicator", which allows me to differentiate.  It does not really help if I can 
use the construct in the constructs I am trying to be  distinct from. 
If the variable declared in the expression list would extend to the attached 
closure, I would see more use in the construct, even though then the 
differentiator problem from before is even bigger.

> Support expression scope variable
> ---------------------------------
>
>                 Key: GROOVY-11525
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11525
>             Project: Groovy
>          Issue Type: New Feature
>            Reporter: Daniel Sun
>            Priority: Major
>
> Let's take a look at the following code first:
> {code}
> int hash(Object key) {
>    int h;
>    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
> }
> {code}
> variable {{h}} is actually an expression scope variable, but current syntax 
> doesn't support.
> I propose an expression scope variable syntax, e.g. 
> Proposed syntax 1:
> {code}
> // Just support declaring variables of same type
> (int h, x, y; ...)
> {code}
> {code}
> int hash(Object key) {
>    // h is only visible in the expression
>    return (int h; (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16))
> }
> {code}
> We can reduce the scope of {{h}} further:
> {code}
> int hash(Object key) {
>    // h is only visible in the expression
>    return (key == null) ? 0 : (int h; (h = key.hashCode()) ^ (h >>> 16))
> }
> {code}
> Proposed syntax 2(I prefer the syntax):
> {code}
> // Support declaring variables of different types
> (int h, X x, Y y -> ...)
> {code}
> {code}
> int hash(Object key) {
>    // h is only visible in the expression
>    return (int h -> (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16))
> }
> {code}
> {code}
> int hash(Object key) {
>    // h is only visible in the expression
>    return (key == null) ? 0 : (int h -> (h = key.hashCode()) ^ (h >>> 16))
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to