简体   繁体   English

Groovy-执行之前的类型检查关闭代码

[英]Groovy - Type Check Closure Code Before Execution

I have a Groovy script that lets the user define some dynamic properties and methods and later executes a user-defined closure. 我有一个Groovy script ,该Groovy script可让用户定义一些动态属性和方法,并稍后执行用户定义的闭包。 A script would look like this: 脚本如下所示:

// init properties and methods dynamically at runtime
context.prop1 = "Some test value"
context.method1 = { String input ->
    "exec " + input.toUpperCase()
}

// "this" is set to the context variable from above
run {
    println method1( prop1 )
}

So in the beginning of the script, a context is initialized with user-defined properties (eg prop1 ) and methods (eg method1 ). 因此,在脚本的开头,将使用用户定义的属性(例如prop1 )和方法(例如method1 )来初始化context The context is then used as this pointer in the run closure. 然后将上下文用作运行闭包中的this指针。 I have achieved this by dynamically extending the meta class of the context and setting the context as delegate of the run closure (with DELEGATE_FIRST as resolves strategy). 我通过动态扩展上下文的元类并将上下文设置为运行闭包的委托(使用DELEGATE_FIRST作为解决策略)来实现此目的。

Currently I am struggling at type checking . 目前,我正在努力进行类型检查 Before executing the run closure, I would like to check if method1 really expects prop1 . 在执行运行关闭之前,我想检查method1真的期望prop1 I have looked into the DelegatesTo annotation, but that doesn't seem to work for dynamically extended objects. 我已经研究了DelegatesTo批注,但这似乎不适用于动态扩展的对象。 I have also played with the AST, but since my knowledge on that topic is limited, I haven't come up with a solution. 我也玩过AST,但是由于我对该主题的了解有限,所以我没有提出解决方案。 If what I want to achieve is possible, any pointers in the right direction would be greatly appreciated. 如果我想实现的目标是可能的,那么朝正确方向的任何指针将不胜感激。

You want to add a method to a context at runtime and then type check this before execution of that method. 您想在运行时将方法添加到上下文中,然后在执行该方法之前键入check this。

Type checking is done at compile time. 类型检查在编译时完成。 That is before anything of your program is executed. 那是在程序任何内容执行之前。 There is normally no chance this can ever check anything that will only happen at runtime, unless you have a way to statically declare it and give the compiler the power to do the check. 通常,除非有办法静态声明它并赋予编译器执行检查的能力,否则通常不可能检查只有在运行时才会发生的任何事情。 But this means normally, you will have to do static compilation. 但这通常意味着您必须进行静态编译。

One way would be to use type checking extensions , but I think in your case that might be overkill. 一种方法是使用类型检查扩展名 ,但是我认为在您的情况下这可能会过大。 A more simple way would be to use extension modules . 一种更简单的方法是使用扩展模块 And the most simple way would be to use custom script base class . 最简单的方法是使用自定义脚本基类

But for any of these solution you will need static compilation to really have type checking, same for DelegatesTo (which is more used in combination with extension modules). 但是对于这些解决方案中的任何一个,您都需要静态编译才能真正进行类型检查,这与DelegatesTo相同(通常与扩展模块结合使用)。 For a type checked DSL a mix of type checking extensions and extension modules can work very well. 对于类型检查的DSL,类型检查扩展和扩展模块的混合可以很好地工作。 But you will of course loose more dynamic features of the language and some simplicity. 但是,您当然会失去该语言的更多动态特性和一些简单性。

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

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