RFC: unboxed 'newtype' module


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 Objects 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.


I would make Make private, and provide an API like

scala> val Meter = Newtype[Double]
Meter: Newtype[Double] = Make()


Thanks, great idea. Updated with this API. The thing I’m trying to figure out now is where to put implicit instances for these new types. Also getting bitten by universal equality:

scala> import newtype.Newtype
import newtype.Newtype

scala> val Cm = Newtype[Double]
Cm: newtype.Newtype[Double] = newtype.Newtype$Make@4f377e61

scala> val oneCm = Cm(1.0)
oneCm: Cm.T = 1.0

scala> oneCm == 1.0
res0: Boolean = true