scala - Is there a way to forbid overriding a member in a mixin? -
so, have bunch of stackable traits defined, this:
trait base { val prefix: string = "" def foo = "" } trait foo extends base { override def foo = prefix + "/foo" + ";" + super.foo } trait bar extends base { override def foo = prefix + "/bar" + ";" + super.foo } class bat extends foo bar { override val prefix: string = "bat" } new bat().foo
the above returns bat/bar;bat/foo
, want. now, problem is, of course, there can 1 instance of prefix
, and, if 1 of mixins tries override it, won't work, because 1 defined in bat
"trumps" it. ok, except, "won't work" thing explicit.
can think of kind of trick, make attempting override prefix
in trait rather in class inheriting cause compilation error?
you can split trait in 2 , leverage explicitly typed self-references, this:
trait base { def foo = "" } trait prefix { val prefix: string = "" } trait foo extends base { this: prefix => override def foo = prefix + "/foo" + ";" + super.foo } trait bar extends base { this: prefix => override def foo = prefix + "/bar" + ";" + super.foo } class bat extends prefix foo bar { override val prefix: string = "bat" } new bat().foo
Comments
Post a Comment