Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is not only because <code>A with B</code> has to be regarded as a new type. For the Scala type system, it does not directly matter if there exists a class corresponding to <code>A with B</code>. An anonymous class is generated because it has to contain <em>bridge</em> methods for all methods in the traits that have been mixed in.</p> <p>The reason that an anonymous class is created is that the object must have implementations of all the methods from <code>A</code> and all the methods from <code>B</code>. On the JVM bytecode level, this would warrant inheriting multiple classes, and the multiple inheritance model is not supported on the JVM.</p> <p>To simulate multiple inheritance (or mixin composition, however you wish to call it), Scala does the following things when you create a trait:</p> <ol> <li>If the trait <code>T</code> has no method implementations, it creates an interface which defines all the methods in the trait.</li> <li><p>If the trait <code>T</code> has method implementations, it additionally creates a class <code>T$class</code> which has a static method for each of the concrete methods in <code>T</code>. This static method has the same body as its corresponding method in <code>T</code>, but its signature is changed to include the <code>this</code> parameter. If <code>T</code> had:</p> <pre><code>def foo(x: Int) = x </code></pre></li> </ol> <p>then <code>T$class</code> will have:</p> <pre><code>&lt;static&gt; def foo($this: T, x: Int) = x </code></pre> <p>The class obtained by mixin composition of some class <code>A</code> and some trait <code>T</code> will then have a special bridge method generated which forwards the call to the static method which contains the body. This way, the body of the method is not duplicated in every class which mixes in <code>T</code>. This is why the anonymous class has to be created - it has to have bridge methods defined for each method in <code>T</code>.</p> <p>Here's an example. When you create a new class by doing mixin composition, e.g. call <code>new A with T</code>:</p> <pre><code>class A { def bar = println("!") } trait T { def foo(x: Int) = x } new A with T </code></pre> <p>the compiler will rewrite it roughly to something like this:</p> <pre><code>class A { def bar = println("!") } &lt;interface&gt; T { def foo(x: Int): Int } class T$class { &lt;static&gt; def foo($this: T, x: Int) = x } class $anon extends A &lt;implements&gt; T { // notice that `bar` is inherited, but `foo` is not &lt;bridge&gt; def foo(x: Int) = T$class.foo(this, x) } new $anon </code></pre> <p>Notice that the compiler could actually rewrite the callsites to <code>foo</code> to call the static methods directly from the callsite, rather than through a bridge method. The reason why it's not done that way is because then it would not support subtyping polymorphism anymore.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload