[ https://issues.apache.org/jira/browse/GROOVY-11525?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17903105#comment-17903105 ]
Jochen Theodorou commented on GROOVY-11525: ------------------------------------------- [~daniel_sun] What is the extend of what you are thinking of here? Something like ((Statement;)* Expr). Or only the initializer variants? Only one Statement or multiple?: {code:Java} def <T> swapHeadAndTail(List<T> list) { return (T h = list[0]; List<T> t=list[1..-1]; [*t, h]) } {code} The alternative would be of course: {code:Java} def <T> swapHeadAndTail(List<T> list) { return {T h = list[0]; List<T> t=list[1..-1]; [*t, h]}() } {code} This requires only 2 characters more syntax wise. If the argument is, this will be inefficient, because it will create a Closure, which is then executed right away... well... that does not have to be the case, does it? > 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)