Is this type parameter miniboxed?
If miniboxed, what is the actual type of a type parameter? It can be any of the scala miniboxing types.
If miniboxed, what is the storage type of a type parameter?
If miniboxed, what is the storage type of a type parameter?
The storage type of a miniboxed type parameter can be one of the
following three types:
* Object
for instantiations with AnyRef-based
types,
erased generic types and value classes
* Long
for instantiations with Unit
, Boolean
, Byte
,
Char
, Short
, Int
and Long
* Double
for instantiations with Float
and Double
The
MiniboxingReflection
object allows reflecting on the type parameters of miniboxed classes. For example, givenclass C[@miniboxed T]
, reflection can determine at run-time whether a type parameter is miniboxed, its instantiation and what type is used to store this value. These methods do not incur any overhead, since they are compiled away by the miniboxing plugin.Let's see an example:
Yet, it's still possible to instantiate the class in an erased context, leading to suboptimal storage. Still, the miniboxing plugin will slap you on the wrist for doing so, and reflection will tell you you're doing it wrong:
Finally, there's a good use case for this, if the class is always supposed to be miniboxed:
The "isMiniboxed" method is also partially evaluated away by the compiler:
In the low-level bytecode you will have:
So there is 0 overhead in using the
isMiniboxed
method. Still,reifiedType
andstorageType
are not partially evaluated and do incur a small overhead.