2009-09-09 11 views
2

J'ai ce code dans un Grails 1.0.4 console Groovy:1.5.x Groovy/Grails 1.0.x If-Else bug déclaration

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" 

Ce qui revient à ceci:

discovering device: 048123456 
true == msisdn: 048123456 ==~ pattern: .*48123456 
true == msisdn: 048123456 ==~ pattern: .*48123456 
true == msisdn: 048123456 ==~ pattern: .*48123456 
false == msisdn: 048123456 ==~ pattern: .*48123457 
found: [] 

Am I manque quelque chose ou est-ce un bug?

EDIT: Je l'ai changé comme ceci:

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 
} 

et maintenant ça marche! La construction if-else groovy ne renvoie-t-elle pas une valeur?

Répondre

2

C'est une fonctionnalité bug/manquant qui a été corrigée dans Groovy 1.6.x, donc ça marchera dans Grails 1.1+. Pour Grails 1.0.x/Groovy 1.5.x, vous devez renvoyer explicitement une valeur de chaque branche if.