class Ball(value:Int):
def number = value
def isMagic = number == 8
trait Magicify:
this:Ball =>
override def number = 8;
def wasMagic:Boolean = ??? // access pre-override value?
List(Ball(1),Ball(8)).map { b => (b.number, b.isMagic) } // As expected
val m = { object $anon extends Ball(1) with Magicify; $anon }
(m.number, m.isMagic) // (8, true)
Is there a more canonical way to do an anonymous mix-in – other than {object $anon extends ...; $anon}?
Also, is there a way to call the original (non-overridden) method number from the mix-in?
It is an implementation restriction on Scala 2 and obviously also Scala 3.
But I think it demonstrates that certain feature interactions remain obscure or under-exercised, and it’s easier to find an idiomatic construction than figure out (for the nth time) how traits work with inheritance.