Groovy 1.5.x / Grails 1.0.x If-Else语句错误
-
05-07-2019 - |
题
我在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分支的值。
不隶属于 StackOverflow