Skip to content

Instantly share code, notes, and snippets.

@purplefox
Created March 23, 2012 08:48
Show Gist options
  • Save purplefox/2168524 to your computer and use it in GitHub Desktop.
Save purplefox/2168524 to your computer and use it in GitHub Desktop.
Groovy question
I have a method on a class which takes a Closure as an argument:
class Foo {
void register(Closure handler)
}
The closure represents a handler that will be called back some time in the future
In most cases users will specify the Closure directly when calling it:
new Foo().register { println "in handler" }
But there are other objects that can act as handlers and that I want to be able to pass to register too
E.g. I would like to do something like
class MyHandler implements Closure {
void call() {
println "in handler"
}
}
But this doesn't work since Closure is not interface. I've tried extending it too, and that doesn't seem to work.
Any ideas?
@purplefox
Copy link
Author

Peter,

yup, makes sense.

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