Tue 13 Jun 2006
Traits > Interfaces
Posted by dkaz under Java, C#, Smalltalk, Programming
Over the last couple of months I’ve spent a bit of time studying Scala, a multi-paradigm (function + oo) programming language designed to inter-operate with both .NET and Java.
One of the (many) neat features of the language is its inclusion of the trait abstract data type. Scala traits are similar to Java interfaces in that they are used to define method signatures for classes in an inheritance hierarchy. In contrast to interfaces, however, they can also include definitions of some of their methods.
trait Similarity { def isSimilar(x: Any): Boolean def isNotSimilar(x: Any): Boolean = !isSimilar(x) }
Traits are not unique to Scala, they have also been tried out in Squeak, Perl and C#.

June 14th, 2006 at 6:55 pm
My concern with traits is that you have to specifically say you want them in your class. I prefer the proposed C# extension method idea where other people can plug into you.
I guess it’s a matter of how many interfaces you want to be compatible with. But if it’s a matter of convenient functionality, I like extension methods better.