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 ، تحتاج إلى إرجاع قيمة بشكل صريح من كل فرع.
لا تنتمي إلى StackOverflow