groovy - 关于省略 'return' 关键字以返回 Groovy 中的闭包 block 的不一致

标签 groovy return closures keyword

我刚刚开始学习 Groovy。我进入了这个:https://groovy-lang.org/style-guide.html#_return_keyword_optional 因此,可以在方法定义的末尾省略“return”。好吧。 尽管我更喜欢亲自使用它,但我还是想看一看。 我测试了一些简单的案例来弄清楚它是如何工作的:

例如演示.groovy:

def a1 = { return { return 'hi'} }()
println a1

def a2 = { return {'hi'} }()
println a2

def a3 = { {'hi'} }()
println a3

def a4 = { {-> 'hi'} }()
println a4

我运行了脚本,结果是:

demo$_run_closure1$_closure5@21c64522
demo$_run_closure2$_closure6@460f76a6
hi
demo$_run_closure4$_closure7@1922e6d

从案例a1开始,我一次省略了关键字“return”。它们是情况 a2a3。 让我摸不着头脑的是a3这个案例。我希望关闭像案例 a1a2 一样返回。但它返回了“hi”,而不是闭包本身。 所以我添加了另一个测试用例 a4 然后它按预期工作。

我试图搜索一些关于它的文档,但找不到合适的文档。 为什么它返回“hi”而不是闭包对象?

提前致谢。

仅供引用,我的常规版本是:

Groovy Version: 3.0.7 JVM: 15.0.1 Vendor: Oracle Corporation OS: Mac OS X

最佳答案

您在 a3 示例中看到的不是嵌套闭包,而是嵌套代码块。

Nested code blocks

An infrequently used structure within Java is the anonymous code block. It’s generally not encouraged as it’s often a sign that refactoring the related code into a method is in order. But it’s sometimes useful to restrict scoping and is now available in Groovy:

{
    def a = 1
    a++
    assert 2 == a
}
try {
    a++ // not defined at this point
} catch(MissingPropertyException ex) {
    println ex.message
}
{
    {
        // inner nesting is another scope
        def a = 'banana'
        assert a.size() == 6
    }
    def a = 1
    assert a == 1
}

Be aware though that in Groovy having a code block looking structure after any method call will be seen as an attempt to pass a closure as the last parameter in the method call. This happens even after a new line. So it’s safe to start an anonymous code block after any other block (e.g. an if-then-else statement or another anonymous code block). Anywhere else and you might need to terminate the previous statement with a semicolon. In which case, see the note above about refactoring your code! :-)


Source: https://groovy-lang.org/releasenotes/groovy-3.0.html#_nested_code_blocks

您可以在 groovyConsole 中运行此脚本,然后转到“脚本”->“检查 AST”以查看编译器在这种情况下生成的内容:

enter image description here

可以看到a3是一个隐式返回'hi'的闭包。

在 Groovy 3 中添加了对嵌套代码块的支持。如果您在 Groovy 2 中运行相同的示例,您将看到以下编译错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/home/wololock/workspace/groovy-3-sandbox/src/main.groovy: 7: Ambiguous expression could be either a parameterless closure expression or an isolated open code block;
   solution: Add an explicit closure parameter list, e.g. {it -> ...}, or force it to be treated as an open block by giving it a label, e.g. L:{...} @ line 7, column 12.
   def a3 = { {'hi'} }()
              ^

1 error

关于groovy - 关于省略 'return' 关键字以返回 Groovy 中的闭包 block 的不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67200693/

相关文章:

java - 使用以前的方法答案调用方法

C - main 中的线程结果

iterator - 如何在切片上返回迭代器?

swift - 有没有办法让一个函数在不重新指定所有参数的情况下调用自己?

grails - 有没有一种简单的方法可以从 Grails 中的远程服务器获取 JSON 数据集?

java - 在 Oracle ADF 12.1.3 中从 transient 属性填充持久属性

grails - 如果 message.properties-code 存在,我如何分支

java - 如何让我的程序(彩票游戏)再次检查提示?

groovy - 如何在 Groovy 数组中 grep 查找 % ?

javascript - 闭包和作用域在运行时如何在 JavaScript 中表示