class

Box(T)

Inherits Reference / Object

A Box allows turning any object to a Void* and back.

A Box's purpose is passing data to C as a Void* and then converting that back to the original data type.

For an example usage, see Proc's explanation about sending Procs to C.

Class methods

box(object : T) : Pointer(Void)

Turns object into a Void*.

If T is not a reference or pointer type, nor a union between reference types and Nil, this method effectively copies object to the dynamic heap.

NOTE: The returned pointer might not be a null pointer even when object is nil.

Source
unbox(pointer : Pointer(Void)) : T

Unboxes a Void* into an object of type T. Note that for this you must specify T: Box(T).unbox(data).

Raises NilAssertionError if pointer is null and T is not nilable.

WARNING: It is undefined behavior to box an object in one type and unbox it via a different type; in particular, when boxing a T and unboxing it as a T?, or vice-versa.

Source