« Return to Thread: mixins etc.

Re: mixins etc.

by Alex Tkachman :: Rate this Message:

Reply to Author | View in Thread

See comments below

On Wed, Apr 9, 2008 at 11:36 AM, Dierk König <dierk.koenig@...> wrote:

>
>
>
> cool ;-)
>
>  Thanks a lot for the implementation and the comprehensive
>  description!
>
>
>  | So what is mixin?
>  |
>  | Mixin is usual category-like class, which provides methods to
>  | extend some (or several) existing classes.
>  |
>  | class ArrayListExt {
>  |    static def newArrayListMethod (ArrayList self) {
>  |         "result of newArrayListMethod "
>  |    }
>  | }
>
>  ... except that categories can be used to define
>  methods on a combination of classes that work together
>  to fulfill a common purpose. E.g.
>  defining something like
>
>  class IntCodec {
>      static String encode (Integer self) { ... }
>      static Integer decode (String self) { ... }
>  }
>
>  is not possible with mixins, since they only affect
>  one class at a time.
>
>  Right?
>

In this case you will need to apply mixin separately to String and Integer.

Initially I had syntax DefaultGroovyMethods.mixin IntCodec but as
Jochen noticed in thread on global categories it brings a bit too much
danger, so right now this functionality is banned. But it is very easy
to return it back :)

>  Also, categories have a scope of use. Mixins don't,
>  right?
>

Right, mixins has no scope.

>  Is there also any difference in the affected threads?
>  (I vaguely remember this has been discussed on the
>  list before... sorry)
>
>

No threading involved. Another difference from categories.

>  | simple use of it
>  |
>  | ArrayList.mixin ArrayListExt
>
>  how about setting the scope like
>
>  ArrayList.mixin (ArrayListExt) {
>      // new methods available here
>  }
>  // but not here anymore
>
>

Assuming that ArrayListExt effects ArrayList (and hierarchy) only it
is equivalent to using category.

>  ?
>
>  | @Mixin(MyClassExt)
>  | class MyClass {
>  |      def result () {
>  |          longRunningJavaMethod ()
>  |      }
>  | }
>  |
>  | and write for example in Java
>  |
>  | public MyClassExt {
>  |          public static Set<Collection> longRunningJavaMethod (MyClass
>  | self) { /// }
>  | }
>
>  so cool and very powerful ;-)
>
>  reminds me on my old Ruby days...
>
>
>
>  | Another nice feature for groovy objects is ability to do per
>  | instance mixins
>
>  dito
>
>
>  | 1) Methods of the class and super classes (including all
>  | their modifications except categories)
>  | 2) DGM methods
>  | 3)
>  | 4) EMC methods
>  | 5) methods of categories in use
>
>  well explained!
>
>  Now how about this:
>
>  class A { def foo() {'A'} }
>  class B extends A { def foo() {'B'} }      // subclass overrides method
>
>  class M { static void foo(A self) {'M'} }  // mixin redefines method
>
>  A.mixin M
>
>  println( new B().foo() )
>
>  ???? %-)
>

In theory expected behaviour is to print 'B' because B defines more
specific method. But new B().super.foo () (not legal syntax of course)
should print 'M' In practice, test case needed :)

>  cheers
>  Dierk

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 « Return to Thread: mixins etc.