-
-
Save ldaley/5429603 to your computer and use it in GitHub Desktop.
Handler<T> handler(@DelegatesTo.Target(asType = true) Class<T> type, @DelegatesTo Closure<?> configurer) { | |
return new Handler() { | |
void handle(T object) { | |
configurer.delegate = object | |
configure.call(delegate) | |
} | |
} | |
} |
(parameters[0] instanceof ClassExpression)?parameters[0].type:getType(parameters[0])
That seems doomed for user error. Let's see if we can avoid having to use imperative code.
I understand that {{asType}} doesn't mean "the type that the Class represents"
It did originally, because I was just thinking about type tokens. That is, asType
would only work on a Class param (anything else would be an error). In that context, I think the name makes sense. But know that we are talking about something bigger, I agree that the name doesn't make sense.
You would have to map Date to E, then E to V.
I wish I could contribute here, but it's beyond my knowledge.
It seems like:
void someMethod(@DelegatesTo.Target(genericTypeIndex=1) Map<K,V> map, @DelegatesTo Closure<?> closure)
Would cover a lot of use cases if it could be made to work.
Agreed. I'll make some experiments around this.
parameters
refer to the actual arguments when you callhandler(Foo) { closure code }
soparameters[0]
refers to theFoo
parameter, and the type checker nows its "flow type" (note that writing this makes me think you would need to do something like:With your second example, I understand that {{asType}} doesn't mean "the type that the Class represents", but the actual generic type argument. That could require complex mappings, but that's in theory doable I think... I would prefer another name than "asType", though, such as "genericType".
For example, imagine you have:
Then you want to map it to:
You would have to map Date to E, then E to V. That's something the type checker does internally, although it touches code that is known to have bugs (due to the complex representation of generic types in Groovy itself).