Scala - how to force a specific order of traits

Whereas the order of the features while mixing them, how can I force them to be ordered in a certain way. For example, I have this:

val t = new Wink with Dash with Right with Left

      

and I want to set conditions, for example if Right NOT Left

, and letDash COMES FIRST THEN Right OR Left

+3


source to share


1 answer


One way to achieve such limitations in the methods of mixing features is as follows:

trait Dash[T <: Dash[T]]
trait Right extends Dash[Right]
trait Left extends Dash[Left]

val t = new Wink with Dash[Right]

      

Thus, it [T <: Dash[T]]

makes us immediately direct the right or left line. (Right or Left in your requirements)



On the other hand, due to expansion, extends Dash[Right]

you cannot mix a right or left stroke without using a dash. (Dash comes first in your requirements)

It also sounds like you are checking some condition to choose between Right or Left. This can be done as follows:

val t = if (p) new Wink with Dash[Right] else new Wink with Dash[Left]

      

+3


source







All Articles