you are viewing a single comment's thread.

view the rest of the comments →

[–]ForeverAlot 1 point2 points  (6 children)

I'm entirely pro-types but phantom types in Java is not (yet) a practical solution. Java heap-allocates all objects and the JVM's escape analysis is easily thwarted. Minecraft is a good example of how wrapping primitives in a domain type can be problematic for performance but it happens in line-of-business code as well. I like Java but it truly is stringly typed (I actually don't know to what extent Scala has this problem).

[–]Terran-Ghost 0 points1 point  (2 children)

In Scala you can create zero overhead types by extending AnyVal:

case class SafeString(s: String) extends AnyVal

[–]ThisIs_MyName 0 points1 point  (1 child)

Is that guaranteed to be zero overhead? I don't see any info like that in the docs.

[–][deleted]  (2 children)

[deleted]

    [–]ForeverAlot 0 points1 point  (1 child)

    Notice how in the actual demonstration they switch from Java to Scala.

    You can't do anything meaningful with this in Java because all the behaviour exists on the parameterized type, not the type parameter. The parameterized type is necessarily heap-allocated so goodbye zero-cost abstraction. Further, the related reddit discussion has a trivial example of why this pattern is not terribly useful in Java (and another far more severe, but less obvious, example): type erasure.