Note that there are some explanatory texts on larger screens.

plurals
  1. PORefer generic without type parameter in Scala
    primarykey
    data
    text
    <p>I am creating a tree structure in Scala, trying to implement the following type restrictions:</p> <ol> <li>Non-root nodes are one of three types - Time Node, Start Node or End Node</li> <li>Root node only has children of type Time Node</li> <li>Time Nodes only have children of type Start Node</li> <li>Start Nodes only have children of type End Node</li> <li>End nodes may have children of type Start Node or Time Node</li> </ol> <p>These are my type definitions:</p> <pre><code>trait TreeNode[U] { val children:HashSet[NonRootNode[U]] def addChild(c:NonRootNode[U]) } class NonRootNode[T &lt;: TreeNode[T]] extends TreeNode[T] { var passengers:Set[Passenger] = Set() val children:HashSet[T] = new HashSet[T] def addChild(c:T) = { children.add(c) } } case class RootNode extends TreeNode[TimeNode] { val children:HashSet[TimeNode] = new HashSet[TimeNode] def addChild(c:TimeNode) = { children.add(c) } } case class TimeNode(time:Int) extends NonRootNode[StartNode] { } case class StartNode(l:Option[String]) extends NonRootNode[EndNode] { } case class EndNode(l:Option[String]) extends NonRootNode { } </code></pre> <p>First, does this implement requirements 1-4 correctly? Second, is there a way to implement requirement 5 here in the definition? Is there ANY way to implement this requirement, as this would require a heterogeneous set to store children references.</p> <p>EDIT: Types RootNode and EndNode would need a method like the following: </p> <pre><code>trait ParentOfTimeNode extends TreeNode{ //type ChildType = TimeNode def addTimeNodes(startTime:Int, maxTime:Int) = { for(i &lt;- startTime to maxTime) { this.addChild(new TimeNode(i)) } } } </code></pre> <p>Without that line commented, the line that screams is: </p> <pre><code>case class EndNode(l:Option[String]) extends NonRootNode with ParentOfTimeNode{ type ChildType = NonRootNode with IntervalMarker } </code></pre> <p>because of an obvious type match. With the line commented, this.addChild screams since it is bound by ChildType which is undefined.</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. 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