Structs

Instead of defining a type with class you can do so with struct:

  1. struct Point
  2. property x, y
  3. def initialize(@x : Int32, @y : Int32)
  4. end
  5. end

Structs inherit from Value so they are allocated on the stack and passed by value: when passed to methods, returned from methods or assigned to variables, a copy of the value is actually passed (while classes inherit from Reference, are allocated on the heap and passed by reference).

Therefore structs are mostly useful for immutable data types and/or stateless wrappers of other types, usually for performance reasons to avoid lots of small memory allocations when passing small copies might be more efficient (for more details, see the performance guide).

Mutable structs are still allowed, but you should be careful when writing code involving mutability if you want to avoid surprises that are described below.

Passing by value

A struct is always passed by value, even when you return self from the method of that struct:

  1. struct Counter
  2. def initialize(@count : Int32)
  3. end
  4. def plus
  5. @count += 1
  6. self
  7. end
  8. end
  9. counter = Counter.new(0)
  10. counter.plus.plus #=> Counter(@x=2)
  11. puts counter #=> Counter(@x=1)

Notice that the chained calls of plus return the expected result, but only the first call to it modifies the variable counter, as the second call operates on the copy of the struct passed to it from the first call, and this copy is discarded after the expression is executed.

You should also be careful when working on mutable types inside of the struct:

  1. class Klass
  2. property array = ["str"]
  3. end
  4. struct Strukt
  5. property array = ["str"]
  6. end
  7. def modify(object)
  8. object.array << "foo"
  9. object.array = ["new"]
  10. object.array << "bar"
  11. end
  12. klass = Klass.new
  13. puts modify(klass) #=> ["new", "bar"]
  14. puts klass.array #=> ["new", "bar"]
  15. strukt = Strukt.new
  16. puts modify(strukt) #=> ["new", "bar"]
  17. puts strukt.array #=> ["str", "foo"]

What happens with the strukt here:

  • Array is passed by reference, so the reference to ["str"] is stored in the property of strukt
  • when strukt is passed to modify, a copy of the strukt is passed with the reference to array inside it
  • the array referenced by array is modified (element inside it is added) by object.array << "foo"
  • this is also reflected in the original strukt as it holds reference to the same array
  • object.array = ["new"] replaces the reference in the copy of strukt with the reference to the new array
  • object.array << "bar" appends to this newly created array
  • modify returns the reference to this new array and its content is printed
  • the reference to this new array was held only in the copy of strukt, but not in the original, so that’s why the original strukt only retained the result of the first statement, but not of the other two statements

Klass is a class, so it is passed by reference to modify, and object.array = ["new"] saves the reference to the newly created array in the original klass object, not in the copy as it was with the strukt.

Inheritance

  • A struct implicitly inherits from Struct, which inherits from Value. A class implicitly inherits from Reference.
  • A struct cannot inherit from a non-abstract struct.

The second point has a reason to it: a struct has a very well defined memory layout. For example, the above Point struct occupies 8 bytes. If you have an array of points the points are embedded inside the array’s buffer:

  1. # The array's buffer will have 8 bytes dedicated to each Point
  2. ary = [] of Point

If Point is inherited, an array of such type should also account for the fact that other types can be inside it, so the size of each element should grow to accommodate that. That is certainly unexpected. So, non-abstract structs can’t be inherited from. Abstract structs, on the other hand, will have descendants, so it is expected that an array of them will account for the possibility of having multiple types inside it.

A struct can also include modules and can be generic, just like a class.