Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional trait mixins

Tags:

scala

traits

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?

like image 782
amaurremi Avatar asked Sep 17 '25 14:09

amaurremi


1 Answers

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
like image 181
Randall Schulz Avatar answered Sep 20 '25 11:09

Randall Schulz