groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From itoy <ncc00...@nifty.com>
Subject @Field behavior in Groovy script
Date Thu, 19 Jul 2018 06:42:39 GMT
Hi all,

I am a Groovy beginner.
I have a question about Groovy script.

Here is a sample.
--
import groovy.transform.*
def flag1 = false
@Field def flag2 = false
println '---1'
println "binding=${binding.variables}"
println '---2'
args.each { arg ->
//for(def arg: args) {
    switch(arg) {
    case '-1': flag1 = true; break
    case '-2': flag2 = true; break
    }
    println "arg: ${arg}, flag1=${flag1}, flag2=${flag2}, binding -> ${binding
.variables}"
}
println '---3'
println "flag1=${flag1}"
println "flag2=${flag2}"
println "binding=${binding.variables}"


C:>groovy test.groovy -1 -2 groovy
---1
binding=[args:[-1, -2, groovy]]
---2
arg: -1, flag1=true, flag2=false, binding -> [args:[-1, -2, groovy]]
arg: -2, flag1=true, flag2=true, binding -> [args:[-1, -2, groovy], flag2:true
]
arg: groovy, flag1=true, flag2=true, binding -> [args:[-1, -2, groovy], flag2:
true]
---3
flag1=true
flag2=false
binding=[args:[-1, -2, groovy], flag2:true]

C:>groovy -version
Groovy Version: 2.5.1 JVM: 1.8.0_171 Vendor: Oracle Corporation OS: Windows 10


Unfortunately, the variable flag2 was not set.
What is so wrong with that?
I can get result as expected when I use "for(def arg: args) {...}" instead of 
"args.each {...}".

Thanks,

itoy

Mime
View raw message