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.
Turns object into a Void*.
Unboxes a Void* into an object of type T.
Reference
Reference
Reference
Object
Object
Object
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.
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.
© 2012–2026 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.19.0/Box.html