Instead of defining a type with class
you can do so with struct
:
struct Point
property x
property y
def initialize(@x, @y)
end
end
The differences between a struct and a class are:
new
on a struct allocates it on the stack instead of the heapA struct can inherit from other structs and can also includes modules. A struct can be generic, just like a class.
A struct is mostly used for performance reasons to avoid lots of small memory allocations when passing small copies might be more efficient.
So how do you choose between a struct and a class? The rule of thumb is that if no instance variable is ever reassigned, i.e. your type is immutable, you can use a struct, otherwise use a class.