简体   繁体   English

Groovy闭包返回值与代码结构无关

[英]Groovy closure return value independent of code structure

I have some code in build.gradle 我在build.gradle中有一些代码

test {
    doFirst {
        def profile = System.getenv("...")
        if (profile == "dev") {
            println "1: if start"
            // ...
            println "2: if end"
        }
    }
}

and last line ("2: if end") executing anyway, even if profile not "dev" 最后一行(“ 2:如果结束”)仍然执行,即使配置文件不是“ dev”

Looks like groovy don't care about code structure: it simple return last line as result of closure 看起来很时髦,不在乎代码结构:它简单地返回最后一行作为闭包的结果

because if I modify code to: 因为如果我将代码修改为:

test {
    doFirst {
        def profile = System.getenv("...")
        if (profile == "dev") {
            println "1: if start"
            // ...
            println "2: if end"
        }
        println "3: after if"
    }
}

Then, this way, if profile not "dev", then all ok - after checking statement groovy execute line with "3: after if" 然后,以这种方式,如果概要文件不是“ dev”,则一切正常-在检查语句groovy之后执行“ 3:if”之后的行
Is this bug or feature? 这是错误或功能吗? :) :)

是的,正如所评论的,这仅在调试器中-groovy可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM