我在Grails 1.0.4 Groovy控制台中有以下代码:
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
}
现在就可以了!杂乱的if-else构造不应该返回值吗?
最佳答案
这是Groovy 1.6.x中修复的错误/缺失功能,因此可以在Grails 1.1+中使用。对于Grails 1.0.x / Groovy 1.5.x,您需要从每个if分支显式返回一个值。
关于grails - Groovy 1.5.x/Grails 1.0.x If-Else语句错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1400412/