Strange Error w Types

View: New views
1 Messages — Rating Filter:   Alert me  

Strange Error w Types

by andrew cooke :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

This isn't critical - I can just add the explicit type - but I was
surprised to see it and even more surprised (although perhaps I should
not be) that explicitly adding an existential type didn't fix it.  So
I'm curious - can anyone explain what the problem is?

The Graph interface I am working with is described at
http://www.jgrapht.org/javadoc/org/jgrapht/Graph.html but all you need
to know here is that the two type parameters are for vertex and edge
(respectively).

Note that the vertex type is *not* encoded in the edge type in any way
- they are completely orthogonal (which may seem odd in practice, when
the method to get the vertex takes an edge as argument, but the type
system shouldn't care about that, should it?)

The code that failed is:

  def fromGraph(graph: Graph[Int, _], cnxn: Connection) = {
    erase(cnxn)
    for (edge <- graph.edgeSet)
      fromNodeIds(graph.getEdgeSource(edge), graph.getEdgeTarget(edge), cnxn)
  }

And this failed too:

  def fromGraph(graph: Graph[Int, X] forSome {type X}, cnxn: Connection) = {
    erase(cnxn)
    for (edge <- graph.edgeSet)
      fromNodeIds(graph.getEdgeSource(edge), graph.getEdgeTarget(edge), cnxn)
  }

with the error:

[error] /home/andrew/projects/personal/src/scala/uykfd/src/main/scala/org/acooke/uykfd/db/Edges.scala:91:
type mismatch;
[error]  found   : X(in method fromGraph) where type X(in method fromGraph)
[error]  required: X(in value $anonfun) where type X(in value $anonfun)
[error]       fromNodeIds(graph.getEdgeSource(edge),
graph.getEdgeTarget(edge), cnxn)

(the first error was similar but with some auto-generated name instead of X).

But this compiled just fine:

  def fromGraph(graph: Graph[Int, DefaultWeightedEdge], cnxn: Connection) = {
    erase(cnxn)
    for (edge <- graph.edgeSet)
      fromNodeIds(graph.getEdgeSource(edge), graph.getEdgeTarget(edge), cnxn)
  }

where the DefaultWeightedEdge is the concrete type used.

Very odd (to me...),
Andrew

PS Scala 2.8.0-20091009.004154