alias

With alias you can give a type a different name:

  1. alias PInt32 = Pointer(Int32)
  2. ptr = PInt32.malloc(1) # : Pointer(Int32)

Every time you use an alias the compiler replaces it with the type it refers to.

Aliases are useful to avoid writing long type names, but also to be able to talk about recursive types:

  1. alias RecArray = Array(Int32) | Array(RecArray)
  2. ary = [] of RecArray
  3. ary.push [1, 2, 3]
  4. ary.push ary
  5. ary # => [[1, 2, 3], [...]]

A real-world example of a recursive type is json:

  1. module Json
  2. alias Type = Nil |
  3. Bool |
  4. Int64 |
  5. Float64 |
  6. String |
  7. Array(Type) |
  8. Hash(String, Type)
  9. end