Mixin is not 100% inheritance

Groovy allows us to simulate multiple inheritance by using @Mixin annotation:

  1.  
  2. class Foo {
  3. def sayFoo(){
  4. "hello from foo"
  5. }
  6. }
  7.  
  8. class Bar {
  9. def sayBar(){
  10. "hello from bar"
  11. }
  12. }
  13.  
  14. @Mixin([Foo, Bar])
  15. class Bas {
  16. }
  17.  
  18. def foo = new Foo()
  19. def bar = new Bar()
  20. def bas = new Bas()
  21.  
  22. assert foo.sayFoo() == bas.sayFoo()
  23. assert bar.sayBar() == bas.sayBar()
  24.  
  25.  

but unfortunately it will only copy methods and properties, and will not provide mixed type, so when you try something like this:

  1.  
  2.  

assertion exception will be throw.

2 Comments

RSS feed for comments on this post.

  1. It’s actually completely natural. Mixins are about “inheriting” implementation only and OO inheritance is about inheriting implementation and *type*. Therefore, mixin is of course not 100% inheritance. It is no inheritance at all, sensus stricte. It’s only implementation passing between classes.

    Comment by Kalin — January 28, 2010 #

  2. Yes, but this is mistake that you can make, especially in Groovy where types are not required, to think about object that use mixin, as a object of mixed type. But i agree this is not inheritance at all.

    Comment by admin — January 28, 2010 #

Sorry, the comment form is closed at this time.

Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds. Valid XHTML and CSS.