What’s the difference between @Delegate and @Mixin AST transformations in Groovy.
Maybe my question has to do with OO and when apply different patterns, but I use both and I can achieve the same behavior.
class Person {
String name = "Clark"
def walk() { "Walk" }
}
@Mixin(Person)
class Superhero {
def fly() { "Fly" }
}
def superman = new Superhero()
assert superman.name == "Clark"
assert superman.walk() == "Walk"
assert superman.fly() == "Fly"
class Person {
String name = "Clark"
def walk() { "Walk" }
}
class Superhero {
@Delegate Person person
def fly() { "Fly" }
}
def superman = new Superhero(person: new Person())
assert superman.name == "Clark"
assert superman.walk() == "Walk"
assert superman.fly() == "Fly"
The behavior is similar, but
@Delegateand@Mixinare implemented completely differently.@Delegategenerates accessor methods at compile time. Superhero will have a method calledwalk()that simply callsperson.walk(). The generated methods can be seen by dumping the Superhero class file withjavap.@Mixin, on the other hand just creates a small stub that mixs in thePersonmethods at runtime. It uses groovy’s meta-object protocol to allow Superhero to respond to Person’s methods. In this case, you won’t see any Person methods inSuperhero.class.@Delegatehas the advantage that the methods are callable from Java and it avoids doing a dynamic invocation. In addition,@Mixincan’t augment the class with properties.