3

While the order of traits metter during their mixing then how I can force their order in a specific way. For example I have this:

val t = new Wink with Dash with Right with Left 

and I want to put conditions, like if Right NOT Left and let say Dash COMES FIRST THEN Right OR Left

1 Answer 1

3

One way to accomplish such restrictions on ways of mixing traits is following:

trait Dash[T <: Dash[T]] trait Right extends Dash[Right] trait Left extends Dash[Left] val t = new Wink with Dash[Right] 

This way, [T <: Dash[T]] forces us to provide either Right or Left trait right away. (Right or Left in your requirements)

On the other hand Due to extension extends Dash[Right], Right or Left trait can not be mixed in without use of Dash. (Dash comes first in your requirements)

It also sounds as if you are checking some condition in order to decide between Right or Left. This could be done like this:

val t = if (p) new Wink with Dash[Right] else new Wink with Dash[Left] 
Sign up to request clarification or add additional context in comments.

3 Comments

your answer is guiding me to think in a solution but still I cannot extend it, generalize: does it mean that is hard to reach a state where I can say new Wink with Dash with Right with Straight with Colon (many stackable traits which order matter) also where aren't allowed to be mixed two traits which functions are alternative (like Left, Straight and Right)
also, Right or Left logically in my implementation doesn't extend nothing from Dash, I wouldn't like to mix it in every other trait just because of this reason. :(
I am sorry, but I think there is no way in writing new Wink with Dash with Right with Straight with Colon and let the compiler do the rest in terms of constrains. Putting restrictions by extending traits is the weapon of choice I would pick here. One has to think of a certain hierarchy though. But maybe someone else might come up with a better idea on handling this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.