I know this is a crowded field, but I’ve tried my hand at a type-safe ‘unboxed newtype’ module for Scala that uses the ‘abstract type is an alias for the wrapped type’ trick, with the other major trick being that I pass in the wrapped type as a type parameter. Here’s the gist: https://gist.github.com/yawaramin/ae4fbaa67ac50963c8743abea8709857
Because type parameters become Object
s on the JVM, though, I’m not sure exactly under which conditions the wrapped values may become boxed. I’m looking at the bytecode for the test
method in my code and not seeing any allocations for the wrapping, so I’m pretty sure it’s working at least in common scenarios like List[MyWrappedType.T]
and method arguments and return values.
Would appreciate some comments on the implementation.