2
votes

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 a.B?

1
Might this be a compiler optimization? Your class Node does not relate to any particulars of 'g' and Scala might see that and generate a single class A#B. What if you add a reference to an attribute of g? - Daniel Langdon
@DanielL. Unfortunately didn't help. I'm still seeing the same issue - Archeg
@DanielL. Sorry, I panicked too early. That was IntellJ Idea error, compiler solves it correctly - Archeg
no worries, it happens :-) - Daniel Langdon
I recommend that you always verify this sort of thing using the command line Scala compiler or SBT ... IntelliJ is very unreliable. - Miles Sabin

1 Answers

1
votes

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.