I want to have classes that can mix only specified traits:
class Peter extends Human with Lawful with Evil
class Mag extends Elf with Chaotic with Neutral
Is in Scala a way to do this?
UPD:
trait Law
trait Lawful extends Law
trait LNeutral extends Law
trait Chaotic extends Law
trait Moral
trait Good extends Moral
trait Neutral extends Moral
trait Evil extends Moral
class Hero .........
class Homer extends Hero with Chaotic with Good
I want to define a Hero class in a way that constrains the client programmer to mix specific traits (Lawful/LNeutral/Chaotic and Good/Neutral/Evil) if he extends the Hero class. And I want to find some other possibilities to restrict/constrain client code like this.
Tough. Try this:
If you want to make it a requirement that a Law type must be extended, then you need to to use self types in some base class or trait:
And there are a few further tweaks to ensure further type safety. The final result might look like this: