Putting this up to garner feedback for a possible SIP.
AnyVal is a great tool and is widely used, there are developers who are unsatisfied with it as a method of writing wrapper types (also known as the newtype pattern) because of the associated runtime cost.
@S11001001 has written an article titled “The High Cost of AnyVal subclasses”, in which he goes through some of the issues (boxing and unboxing penalty, O(n) complexity for wrapping and unwrapping containers), and argues convincingly that contrary to popular belief, the issues are not caused by Scala’s targeting of the JVM.
Instead, these runtime costs are because of the need to incorporate:
support for isInstanceOf, “safe” casting, implementing interfaces, overriding AnyRef methods like toString, and the like.
- A fully erased newtype/wrapper type mechanism that aims to be completely penalty-free at runtime.
- Since performance is a primary objective, support for features that stand in the way may need to be dropped.
In terms of changes to the language, I think it could live along side
AnyVal (e.g. we don’t necessarily need to get rid of
AnyVal or change its behaviour). It might make sense to have it as a separate thing entirely, like
newtype Label(s: String).