Question

J'ai ce code dans une console 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"

Ce qui retourne 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: []

Est-ce que je manque quelque chose ou est-ce un bug?

EDIT: je l’ai changé comme suit:

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 groovy if-else ne renverra-t-elle pas une valeur?

Était-ce utile?

La solution

C'est un bug / une fonctionnalité manquante qui a été corrigé dans Groovy 1.6.x, donc cela fonctionnera dans Grails 1.1+. Pour Grails 1.0.x / Groovy 1.5.x, vous devez explicitement renvoyer une valeur à partir de chaque branche if.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top