Suppose I want to have a class like Java Date
. Its only data member is a long which represents the milliseconds since 1970.
Would/Could it be of any performance benefit of just making a new Scala type:
type PrimitiveDate = Long
Then you can add methods by using implicit conversion, like it is done for int with RichInt
. Does this "boxing" of the primitive type into a rich class involve any overhead (class creation)? Basically you could just have a static method
def addMonth(date: PrimitiveDate, months: Int): PrimitiveDate = date + 2592000000 * months
and let the type system figure out that it has to be applied when d addMonth 5
appears inside your code.
Edit
It seems that the alias you create by writing type PrimitiveDate = Long
is not enforced by the scala compiler. Is creating a proper class, enclosing the Long, the only way to create an enforced type in Scala?
How useful do you consider being able to create an enforced type alias for primitive types?