amaurremi
amaurremi

Reputation: 787

Conditional trait mixins

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?

Upvotes: 4

Views: 604

Answers (1)

Randall Schulz
Randall Schulz

Reputation: 26486

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

Upvotes: 4

Related Questions