title: Recursive Types in Scala with a List
body: <p>Similarly to <a href="https://stackoverflow.com/questions/11834928/mutually-recursive-types-in-scala">mutually recursive types in scala</a> I am trying to create a mutually recursive type in Scala.</p>
  
  <p>I am trying to make a graph defined with this type (which does compile) :</p>
  
  <pre><code> case class Node(val id : Int, val edges : Set[Node])
  </code></pre>
  
  <p>But I don't understand how I can actually create something with this type, because in order to initialize Node A with edges B and C, I need to at least have  a lazy reference to B and C, but I can't simultaneously create their edgesets.</p>
  
  <p>Is it possible to implement this recursive type?</p>
  
  <p>EDIT:</p>
  
  <p>Here is the solution I am currently using to convert an explicit adjacency list to a self-referential one.</p>
  
  <pre><code>def mapToGraph(edgeMap : Map[Int, mutable.Set[Int]]) : List[Node] = {
    lazy val nodeMap = edgeMap map  (kv =&gt; (kv._1, new Node(kv._1, futures.get(kv._1).get)))
    lazy val futures : Map[Int, Set[Node]] = edgeMap map (kv =&gt; {
      val edges = (kv._2 map (e =&gt; nodeMap.get(e).get)).toSet
      (kv._1, edges)
    })
    val eval = nodeMap.values.toList
    eval //to force from lazy to real - don't really like doing this
  }
  </code></pre>
  
  <p>or alternatively, fromm an edgelist</p>
  
  <pre><code>//reads an edgeList into a graph
  def readEdgelist(filename : String) : List[Node] = {
    lazy val nodes = new mutable.HashMap[Int, Node]()
    lazy val edges = new mutable.HashMap[Int, mutable.Buffer[Node]]()
    Source.fromFile(filename).getLines() filter (x =&gt; x != None) foreach {edgeStr =&gt;
      val edge = edgeStr.split('\t')
      if (edge.size != 2) goodbye("Not a well-formed edge : " + edgeStr + " size: " + edge.size.toString)
      val src = edge(0).toInt
      val des = edge(1).toInt
      if (!(nodes.contains(src))) nodes.put(src, new Node(src, futures.get(src).get))
      if (!(nodes.contains(des))) nodes.put(des, new Node(des, futures.get(des).get))
      edges.put(src, edges.getOrElse(src, mutable.Buffer[Node]()) += nodes.get(des).get)
    }
    lazy val futures : Map[Int, Set[Node]] = nodes map {node =&gt; (node._1, edges.getOrElse(node._1, mutable.Buffer[Node]()).toSet)} toMap
    val eval = nodes.values.toList
    eval
  }
  </code></pre>
  
  <p>Thanks everyone for the advice!</p>