I've heard that Scala has path-dependent types. It's something to do with inner-classes but what does this actually mean and why do I care?
+17
A:
My favorite example:
case class Board(length: Int, height: Int) {
case class Coordinate(x: Int, y: Int) {
require(0 <= x && x < length && 0 <= y && y < height)
}
val occupied = scala.collection.mutable.Set[Coordinate]()
}
val b1 = Board(20, 20)
val b2 = Board(30, 30)
val c1 = b1.Coordinate(15, 15)
val c2 = b2.Coordinate(25, 25)
b1.occupied += c1
b2.occupied += c2
// Next line doesn't work
b1.occupied += c2
So, the type of Coordinate
is dependent on the instance of Board
from which it was instantiated. There are all sort of things that can be accomplished with this, giving a sort of type safety that is dependent on values and not types alone.
Daniel
2010-04-22 21:22:24