How are Scala traits compiled into Java bytecode?

I’m not an expert, but here is my understanding:

Traits are compiled into an interface and corresponding class.

trait Foo {
  def bar = { println("bar!") }
}

becomes the equivalent of…

public interface Foo {
  public void bar();
}

public class Foo$class {
  public static void bar(Foo self) { println("bar!"); }
}

Which leaves the question: How does the static bar method in Foo$class get called? This magic is done by the compiler in the class that the Foo trait is mixed into.

class Baz extends Foo

becomes something like…

public class Baz implements Foo {
  public void bar() { Foo$class.bar(this); }
}

Class linearization just implements the appropriate version of the method (calling the static method in the Xxxx$class class) according to the linearization rules defined in the language specification.

Leave a Comment