简体   繁体   中英

Path-dependent types in scala

I have the following:

class Graph {
  case class Node(val a: Int)
  def createNodes = Node(5) :: Node(7) :: Nil
}

def test(g: Graph): List[g.Node] = g.createNodes

Then I do:

def test2(g: Graph) = {
  val nodes = test(g)
}

I was expecting for the compiler to track that nodes is of type List[g.Node] , but it seems to unable to do that and assigns its type to List[Graph#Node] . If I try type ascription, it still does not help: val nodes: List[g.Node] = test(g)

I feel like I'm missing something important behind path-dependent types in scala. How can I make test(g) return List[g.Node] and why scala needs path-dependent types converted to A#B if almost always we need aB ?

Ahhh. It appears to be a bug of IntellJ Idea. It highlights it as an error, but the compiler resolves it correctly. Type ascription is not needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM