alias
С помощью alias можно дать типу другое имя:
alias PInt32 = Pointer(Int32)
ptr = PInt32.malloc(1) # : Pointer(Int32)
Каждый раз, когда вы используете псевдоним, компилятор заменяет его типом, на который он ссылается.
Псевдонимы полезны, чтобы не писать длинные имена типов, а также чтобы можно было описывать рекурсивные типы:
alias RecArray = Array(Int32) | Array(RecArray)
ary = [] of RecArray
ary.push [1, 2, 3]
ary.push ary
ary # => [[1, 2, 3], [...]]
Реальный пример рекурсивного типа — JSON:
module Json
alias Type = Nil |
Bool |
Int64 |
Float64 |
String |
Array(Type) |
Hash(String, Type)
end
To the extent possible under law, the persons who contributed to this workhave waived
all copyright and related or neighboring rights to this workby associating CC0 with it.
https://crystal-lang.org/reference/1.2/syntax_and_semantics/alias.html