Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The singleton types and ETSR's do not solve the problem. I myself was looking for just the same feature in Scala, but apparently it lacks the so-called self-type annotations.</p> <p>There are circumstances where such self-type annotations could be very useful. Consider an example (adapted from <a href="https://stackoverflow.com/questions/2059601/circular-type-parameters-definition-in-scala" title="Circular type parameters question example">Circular type parameters question example</a>):</p> <pre><code>// we want a container that can store elements trait Container[E &lt;: Element[E]] { def elements: Seq[E] def add(elem: E): Unit } // we want elements be aware of their enclosing container trait Element[E &lt;: Element[E]] { def container: Container[E] } </code></pre> <p>Let's say you put that into a library. A library consumer should do the following:</p> <pre><code>object PersonContainer extends Container[Person] { // actual implementation is not important def elements = Nil def add(p: Person) = {} } class Person extends Element[Person] { // {1} def container = PersonContainer } </code></pre> <p>It is allright and everything works quite as expected. The only thing that concerns is that a library consumer is <em>supposed</em> to use the self-bound type parameter (#1 in the code). But that's not all. Now suppose you have some sort of an ActiveRecord pattern in mind, and you want to add the method <code>save</code> to <code>Element</code>, which just delegates to it's container's <code>add</code> method. Surprisingly, it is not that easy:</p> <pre><code>trait Element[E &lt;: Element[E]] { def container: Container[E] def save() = container.add(this) // won't compile } found : Element[E] required: E </code></pre> <p>Intuitively, we have a few options here:</p> <ul> <li>make <code>add</code> method accept <code>Element[E]</code> instead of <code>E</code>;</li> <li>cast <code>this</code> to <code>Element[E]</code>.</li> </ul> <p>None of these options are satisfactory, just because of the fact that <code>E</code> is not the same as <code>Element[E]</code> (implementations are not forced to use self-bound type parameters). The only way I see of solving this problem is to have that self-type concept in Scala (let's suppose we have it in our favorite language):</p> <pre><code>trait Container[E &lt;: Element] { def elements: Seq[E] def add(elem: E): Unit } trait Element { // the type parameter would be redundant ... def save() = container.add(this) // ... and this would be possible, too, ... def container: Container[this] // ... if only we could do this } </code></pre> <p>If the compiler could treat <code>this</code> (or maybe another keyword), when it is used inside square brackets, as the type of the actual implementation (i.e. the same type as the result of <code>obj.getClass</code>), then the problems would disappear. </p> <p>P.S. May someone consider including this stuff into Scala wishlist? Unfortunately, I don't know, how hard it is to implement such logic since there could be the problems with the notorious JVM's erasure.</p> <p>P.P.S. Or maybe there is some another Scala-way I'm unaware of?</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. This table or related slice is empty.
    1. 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