Values and references – using structs
By default, Crystal objects are allocated into memory and are managed by a garbage collector. This means that you don't have to worry about where each object is in memory and how long it should live – the runtime will take care of accounting for which objects are still referred to by some variables and will release all others, automatically freeing resources. Variables will not store the object per se – it will store a reference pointing to the object. It all works transparently and there is no need to worry about it.
The aforementioned is true for all objects that are created from classes; the types of these objects are reference types. But there is another kind of object: value types.
In the following diagram, you can see the inheritance chain of some types. The ones that are references inherit from the Reference
class, while the ones that are values inherit from the Value
struct. All of them inherit from the special...