grails - Groovy 1.5.x/Grails 1.0.x If-Else语句错误

标签 grails groovy if-statement

我在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,您需要从每个if分支显式返回一个值。

关于grails - Groovy 1.5.x/Grails 1.0.x If-Else语句错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1400412/

相关文章:

c - 所有条件都相等的 If 语句

java - if循环不会停止循环

oracle - GRAILS -Oracle ORA-00904:无效的标识符

grails - 在GORM中的User域的子类上是否允许使用findByUsername

grails - Json调用Rest URL到Grails GSP页面

grails - 在groovyx.net.http.HTTPBuilder上设置其他超时

groovy - Groovy Abstract ConcurrentMap 中的错误?

javascript - 条件评估时间(Javascript)

json - Grails:从请求中解析 JSON 的简单有效的方法

Jenkins 管道阶段 - 传递整个文件