3 ms·
That has nothing to do with type erasure. Type aliases are not distinct from the underlying type, even at compile time. You needed value classes, not aliases.
by kod 10y ago
That has nothing to do with type erasure. Type aliases are not distinct from the underlying type, even at compile time. You needed value classes, not aliases.
example:
object Example {
type IdA = Long
type IdB = Long
def usesA(i: IdA) = "it's an A"
case class IdC(i: Long) extends AnyVal
case class IdD(i: Long) extends AnyVal
def usesC(i: IdC) = "it's a C"
val a: IdA = 1L
val b: IdB = 2L
val c = IdC(3L)
val d = IdD(4L)
usesA(a)
usesA(b)
usesC(c)
// these won't compile
// usesC(d)
// usesC(a)
}
- philipkglass 10y agoHow embarrassing! Thanks for correcting my mistake. I inherited someone else's code base and I thought that the type aliases were there to enforce correctness.