Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>[Note: I am not recommending that you do this.] There's a fair chance you can accomplish what you want. The cast to this.type is a lie, but the JVM doesn't know that and can't throw an exception because the singleton type is a scala concept.</p> <p>Now if you're actually using the singleton property of this.type anywhere, this will get you in trouble in a hurry. But if all you want to do is get covariant return types without all the trouble of typing them, with only the small downside of the huge ugly cast all over the place:</p> <pre><code>trait Expression { def left : Expression def right : Expression def new_with_changes(l : Expression, r : Expression) : this.type } case class Derived1(left : Expression, right : Expression) extends Expression { def new_with_changes(l : Expression, r : Expression) = Derived1(left, right).asInstanceOf[this.type] def foo() = "Derived1" } case class Derived2(left : Expression, right : Expression) extends Expression { def new_with_changes(l : Expression, r : Expression) = Derived2(left, right).asInstanceOf[this.type] def bar() = "Derived2" } </code></pre> <p>And in action:</p> <pre><code>scala&gt; Derived1(Derived1(null,null), null) res0: Derived1 = Derived1(Derived1(null,null),null) scala&gt; res0.new_with_changes(res0, null).bar &lt;console&gt;:6: error: value bar is not a member of Derived1 res0.new_with_changes(res0, null).bar ^ scala&gt; res0.new_with_changes(res0, null).foo res2: java.lang.String = Derived1 scala&gt; Derived2(Derived2(null, null), null) res3: Derived2 = Derived2(Derived2(null,null),null) scala&gt; res3.new_with_changes(null, res3).foo &lt;console&gt;:6: error: value foo is not a member of Derived2 res3.new_with_changes(null, res3).foo ^ scala&gt; res3.new_with_changes(null, res3).bar res6: java.lang.String = Derived2 </code></pre>
    singulars
    1. This table or related slice is empty.
    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