Groovy 1.5.x / Grails 1.0.x If-Else Error Error

I have this code in Grails 1.0.4 Groovy console:

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"

      

Which returns this:

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 missing something or is this a bug?

EDIT: I changed it like this:

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
}

      

and now it works! Don't try to build a Groovy if-else construct?

+2


source to share


1 answer


This is a bug / missing feature that was fixed in Groovy 1.6.x so it will work in Grails 1.1+. For Grails 1.0.x / Groovy 1.5.x, you need to explicitly return a value from each if branch.



+2


source







All Articles