This module is a set of types and functions for converting any object (value or heap) into a generic box type, allowing the user to pass that object around without knowing what's in the box, and then allowing him to recover the value afterwards.
Example:
Box b = box(45);
real r = unbox!(real)(b); |
That is the basic interface and will usually be all that you need to understand. If it cannot unbox the object to the given type, it throws UnboxException. As demonstrated, it uses implicit casts to behave in the exact same way that static types behave. So for example, you can unbox from int to real, but you cannot unbox from real to int: that would require an explicit cast.
This therefore means that attempting to unbox an int as a string will throw an error instead of formatting it. In general, you can call the toString method on the box and receive a good result, depending upon whether std.string.format accepts it.
Boxes can be compared to one another and they can be used as keys for associative arrays.
There are also functions for converting to and from arrays of boxes.
Example:
Box[] a = boxArray(1, 45.4, "foobar");
TypeInfo[] arg_types;
void* arg_data;
boxArrayToArguments(a, arg_types, arg_data);
a = boxArray(arg_types, arg_data); |
One use of this is to support a variadic function more easily and robustly; simply call "boxArray(_arguments, _argptr)", then do whatever you need to do with the array.
|