그루비 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