Stackable Traits Pattern in Scala
code.sahebmotiani.com
code.sahebmotiani.com
For my part I still prefer composition over inheritance and prefer to do composition "by hand" (i.e. explicit members and delegation) where necessary; I wouldn't use this style at all. But Scala's great strength (and great weakness) is that it has absolutely first-class support for traditional OO like this as well as all the functional stuff.
I'm also not 100% sure it's correct to say that ML-style languages have no problem with type-inference. I'm sure Robin Milner would have preferred having full, unrestricted parametric polymorphism (i.e. System F) in ML. Alas System F has undecidable type inference [2]. Hindley-Milner is a restriction of full parametric polymorphism that enjoys type inference, and is expressive enough to type many programs. BC Pierce says that the Hindley-Milner "has hit a sweetspot". I concur.
[1] M. P. Jones, A system of constructor classes: overloading and implicit higher-order polymorphism. http://www.cs.tufts.edu/comp/150GIT/archive/mark-jones/fpca9...
[2] J. B. Wells, Typability and type checking in System F are equivalent and undecidable. http://www.macs.hw.ac.uk/~jbw/papers/f-undecidable-APAL.ps.g...
For those of us who don't know anything about Game of Thrones, and can't pronounce Elvish names, the example in this article reads like:
Flarg → Twipptch → Glorp → Boop → Fwyxnth → AnyRef → Any
So instead of being less boring, I have to use a decoder ring to figure out what's going on here.
//Legacy
abstract class Lannister
//Father
trait Tywin extends Lannister
could have just been: abstract class Family
trait Father extends Family
Otherwise, I appreciate the article - it was informative.It's one of many ways in Scala where a single abstract construct could have several different semantics applied to it.
I understand what you wanted to show but your example is really confusing and definitely is not correct.
http://blog.ryannedolan.info/2014/03/scala-pattern-stackable...
Composition locks away the behavior you want so that you know exactly what you're getting.
Indeed, you could write a straightforward mechanical method to convert any trait stacking into a decoration.