groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan Lukavský <>
Subject Re: Getting return type of generated Closure at runtime
Date Wed, 20 Jan 2021 08:23:27 GMT
Hi Jochen,

yes, I agree that the type information _could_ be stored in the generics 
(after all the Closure is a named class, the compiler only does not 
provide the type information there). Can you point me out how to 
traverse the AST? I have tried that using ClassNode, but I didn't 
succeed in getting the return type from there either.

Many thanks,


On 1/19/21 7:29 PM, Jochen Theodorou wrote:
> On 19.01.21 09:28, Jan Lukavský wrote:
>> Hi OC,
>> I'm pretty sure Closure has a return type, that is what the type
>> parametr V in Closure<V> stands for. When source is compiled using at
>> least @TypeChecked, the return type (e.g. long as in the example I gave)
>> should be known and (I suppose) accessible during runtime somehow.
> There is a return type inferred by the static compiler for a Closure in
> Groovy. But the compiler is of course at compile time, not runtime.
> Though in case of
>>>>>>     String script = "def a = { 1L }";
>>>>>>     Closure<?> closure = (Closure<?>) compile(script).run();
> you could of course have access to the compiler. Basically you would
> progress the AST and then find the Closure node and get the inferred
> return type from it..
> I am not 100% sure, but I think we do not store the type in the generics
> information, otherwise there would be another potential way
> bye Jochen

View raw message