3 ms·
There are a lot of young developers that have never seen the <1.4 Java code and understand what's happening underneath with type erasures until this bites them.
by djsumdog 10y ago
There are a lot of young developers that have never seen the <1.4 Java code and understand what's happening underneath with type erasures until this bites them.
I hope professors today teach the 1.4 vs 1.5+ equivalents and explain what happens in the underlying type system.
A lot of these problems with the JVM are exacerbated in the Scala world. With such a heavy dependence on type matching in Scala, things like
a match { case List[MyCustomType] =>{}; case List[SomeOtherType] => ...
become impossible since the underlying types are removed at compile time.
- philipkglass 10y agoAnd you may not discover the type erasure problems until runtime. I once found an "impossible" bug in production that came of someone accidentally swapping two values passed to a database function. We had separate DocumentId and UserId types for the values -- how could the bad line of code even compile?! Oh, type erasure; they were both Longs "underneath". (This weakness also mandates more basic testing for Scala code than you might expect for a language whose flagship company was called Typesafe.) Yes, the Scala docs do explain that type aliases are not really distinct types, so this sort of bug could have been expected in advance. I came to Scala with no prior Java experience so I got to discover the ways the JVM leaks through on the job. I thought that I could reuse my previous knowledge about types in program design in obvious ways and just get the obviously (to me, anyway) desired behavior. Mea culpa!
- deleted 10y ago[deleted]
- kod 10y agoThat 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.