Say I have a class A
that should be mixed in with trait B
, where B
should be either B1
or B2
based on a flag b1
:
val b1: Boolean
type B = if (b1) B1 else B2 // impossible Scala code
class A extends B
Is there a way to "dynamically" mixin a trait based on a condition?
Types are static things with definitions fixed at compile time. You can create instances of variant anonymous classes using if / else logic, though:
val a = if (b) new A with B1 else new A with B2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With