groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jochen Theodorou <blackd...@gmx.org>
Subject Re: Getting return type of generated Closure at runtime
Date Tue, 19 Jan 2021 18:29:47 GMT
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


Mime
View raw message