Extension methods vs. Traits
Howard Lovatt
howard.lovatt at iee.org
Fri Dec 11 04:45:51 PST 2009
Hi All,
Traits as proposed, http://www.artima.com/weblogs/viewpost.jsp?thread=220916,
are slightly different than Scala Traits:
1. In Scala if you add a new method to a Trait then you need to recompile
classes that use that Trait for the new method to become available, in the
proposal the linker adds the new method automatically (hence no need to
recompile). Note if you just change an existing method in Scala, or for that
matter with the Trait proposal, you don't need to recompile (this is not
quite what Alex said - but I think this is what he meant - Alex please
confirm).
2. In Scala you can only access your immediate super Trait, in
the proposal you can access any super Trait (this was one of the original
motivations behind Traits as opposed to mixins -
http://www.iam.unibe.ch/~scg/Archive/Papers/Scha03aTraits.pdf)
3. In Scala the traits are like mixins with complicated rules for what gets
called if the same trait appears multiple times in a hierarchy (diamond
problem), in the proposal you have to override any method that
is ambiguous (thus much simplifying both the implementation and use of
Traits).
4. Like Scala: the proposal is mainly a modification to the compiler, I
would suggest that the compiler work would be similar to adding extension
methods. Unlike Scala: in addition to the compiler the linker needs to be
changed, so probably on balance slightly more work than extension methods.
I think Traits, as proposed in the blog referenced, would be superior to any
of the extension methods (thats why I made the suggestion!) and I would be
happy to work on their implementation if people thought it worthwhile. On
the other hand, if for all intents and purpose the decision has been made to
implement extension methods then there is no point working on Traits.
What do people think?
-- Howard.
More information about the lambda-dev
mailing list