2
我有這樣的代碼在一個Grails 1.0.4 Groovy的控制檯:的Groovy 1.5.x的/ Grails的1.0.x的if-else語句錯誤
def devices = Device.getAll()
def found = devices.findAll {
if(it?.localNumber && it?.areaCode){
def pattern = ~".*${it.areaCode + it.localNumber}"
def matches = "$msisdn" ==~ pattern
println "$matches == msisdn: $msisdn ==~ pattern: $pattern"
matches
} else {
false
} // if-else
}
println "found: $found"
它返回這樣的:
discovering device: 048123456
true == msisdn: 048123456 ==~ pattern: .*48123456
true == msisdn: 048123456 ==~ pattern: .*48123456
true == msisdn: 048123456 ==~ pattern: .*48123456
false == msisdn: 048123456 ==~ pattern: .*48123457
found: []
上午我錯過了什麼,或者它是一個錯誤?
編輯:我改變這樣的:
def found = devices.findAll {
def matches = false
if(it?.localNumber && it?.areaCode){
def pattern = ~".*${it.areaCode + it.localNumber}"
matches = "$msisdn" ==~ pattern
println "$matches == msisdn: $msisdn ==~ pattern: $pattern"
} else {
matches = false
} // if-else
matches
}
和現在的作品!不是groovy if-else構造返回一個值嗎?