简体   繁体   English

Groovy 闭包 $v

[英]Groovy closure $v

@CompileDynamic
ClassNode buildTemplateClass(ClassNode reference) {
    def methodCount = constX(reference.methods.size())
    def fieldCount = constX(reference.fields.size())

    return  new MacroClass() {

        class Statistics {

            java.lang.Integer getMethodCount() {
                return $v { methodCount }
            }

            java.lang.Integer getFieldCount() {
                return $v { fieldCount }
            }
        }
    }
}

I am not able to get my head around this construct $v { fieldCount } .我无法$v { fieldCount }这个构造$v { fieldCount } What does it mean and why is this needed here ?这是什么意思,为什么需要这里? For a full reference of what the code is doing refer here .有关代码正在执行的操作的完整参考,请参阅此处

In case it is not clear what the constX method does refer here .如果不清楚 constX 方法在这里指的是什么。

As it's written in your doc , $v used inside a macro for substituting an expression正如您的文档中所写, $v在宏中用于替换表达式

In order to substitute any expression inside the macro we need to use the $v method.为了替换宏中的任何表达式,我们需要使用 $v 方法。 $v receives a closure as an argument, and the closure is only allowed to substitute expressions, meaning classes inheriting org.codehaus.groovy.ast.expr.Expression. $v 接收一个闭包作为参数,该闭包只允许替换表达式,即继承 org.codehaus.groovy.ast.expr.Expression 的类。

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

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