Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of impact does applying all these Scala traits have at runtime?

Tags:

scala

traits

Imagine this:

val myObject = if(someCondition) {
    new Whatever with Trait1
} else if(otherCondition) {
    new Whatever with Trait2 with Trait3 with Trait4
} else {
    new Whatever with Trait5
}

Is the myObject object "composed" at runtime, or is the scala compiler smart enough to generate the appropriate code at compile time? What kind of performance impact will it have on the code if you have multiple places that are applying traits like in the above code?

like image 491
Senthess Avatar asked Jun 30 '11 16:06

Senthess


1 Answers

It's composed at compile-time

The traits will be added as interfaces to the resulting type, and any concrete methods from those traits will (usually) be copied to the class in their entirety.

Occasionally, the compiler may have to provide concrete implementations via forwarders to static methods, but this isn't usually the case.

like image 151
Kevin Wright Avatar answered Sep 30 '22 15:09

Kevin Wright