Skip to content

Instantly share code, notes, and snippets.

@ldaley
Last active March 5, 2018 14:59
Show Gist options
  • Save ldaley/5429603 to your computer and use it in GitHub Desktop.
Save ldaley/5429603 to your computer and use it in GitHub Desktop.
Groovy @DelegatesTo and type tokens.
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)
}
}
}
@ldaley
Copy link
Author

ldaley commented Apr 22, 2013

(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.

@melix
Copy link

melix commented Apr 22, 2013

Agreed. I'll make some experiments around this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment