2009-09-09 49 views
2

我有這樣的代碼在一個Grails 1.0.4 Groovy的控制檯:的Groovy 1.5.x的/ Grails的1.0.x的if-else語句錯誤

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 
} 

和現在的作品!不是groovy if-else構造返回一個值嗎?

回答

2

這是一個在Groovy 1.6.x中修復的bug /缺失功能,所以它可以在Grails 1.1+中工作。對於Grails 1.0.x/Groovy 1.5.x,你需要顯式地從每個if分支返回一個值。