Note that there are some explanatory texts on larger screens.

plurals
  1. POScala class inheritance
    primarykey
    data
    text
    <p>Tagged as homework. I'm having trouble in the object oriented world while trying to implement a class.</p> <p>I'm implenting various functions to take action on lists, that I'm using to mock a set. I'm not too worried about my logic on how to find union, for example, but really just the structure.</p> <p>For eg:</p> <pre><code>abstract class parentSet[T] protected () { def union(other:parentSet[T]):parentSet[T] } </code></pre> <p>Now I want a new class extending parentSet:</p> <pre><code>class childSet[T] private (l: List[T]) extends parentSet[T] { def this() = this(List()) private val elems = l val toList = List[T] =&gt; new List(l) def union(other:parentSet[T]):childSet[T] = { for (i &lt;- this.toList) { if (other contains i) {} else {l :: i} } return l } } </code></pre> <p>Upon compiling, I receive errors such that type childSet isn't found in def union, nor is type T to keep it parametric. Also, I assume my toList isn't correct as it complains that it isn't a member of the object; to name a few.</p> <p>Where in my syntax am I wrong?</p> <p><strong>EDIT</strong></p> <p>Now I've got that figured out:</p> <pre><code> def U(other:parentSet[T]):childSet[T] = { var w = other.toList for (i &lt;- this.toList) { if (!(other contains i)) {w = i::w} } return new childSet(w) </code></pre> <p>}</p> <p>Now, I'm trying to do the same operations with map, and this is what I'm working on/with:</p> <pre><code>def U(other:parentSet[T]):MapSet[T] = { var a = Map[T,Unit] for (i &lt;- this.toList) { if (!(other contains i)) {a = a + (i-&gt;())} } return new MapSet(elems + (a-&gt;())) } </code></pre> <p>I still want to use toList to make it easily traversable, but I'm still getting type errors while messing with maps..</p>
    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. 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