Jan,

as always, I might be missing something of importance, but I believe a Closure 
does not have a return type at all (or, more precisely, the return type of all 
Closures is, by definition, Object).

After all, is there a way to define a closure which returns a non-object, say, 
int? I think not.

All the best,
OC

> On 18. 1. 2021, at 9:55 AM, Jan Lukavský <je...@seznam.cz> wrote:
> 
> Hi,
> 
> resending this to dev@ (originally users@). Would anyone know if what I'm 
> trying to achieve is possible? I think the compiler has enough information 
> and (using either @TypeChecked or @CompileStatic) it should be possible to 
> get the return type of Closure at runtime.
> 
> Thanks for any comments,
> 
>  Jan
> 
> On 1/12/21 10:36 AM, Jan Lukavský wrote:
>> Hi,
>> 
>> I'm struggling with something I believe should be possible - when a Closure 
>> is parsed at runtime, it should be possible to retrieve its generic return 
>> type.
>> 
>> I have the following (in Java):
>> 
>>   @Test
>>   public void testClosureReturnTypeExtraction() {
>>     String script = "def a = { 1L }";
>>     Closure<?> closure = (Closure<?>) compile(script).run();
>>     Class<?> returnType = retrieveReturnTypeFrom(closure);        // how to 
>> implement this??
>>     assertEquals(Long.class, returnType);
>>   }
>> 
>> What I cannot figure out is how to extract the type information from the 
>> generated closure. I believe the type information should be there (or at 
>> least it should be possible to put it there). So far, my best attempt was, 
>> that I got that the type argument is "V". :-)
>> 
>> Any pointers would be appreciated.
>> 
>> Cheers,
>> 
>>  Jan
>> 

Reply via email to