An Array is a generic type containing elements of a type T
. It is typically created with an array literal:
[1, 2, 3] # Array(Int32) [1, "hello", 'x'] # Array(Int32 | String | Char)
An Array can have mixed types, meaning T
will be a union of types, but these are determined when the array is created, either by specifying T or by using an array literal. In the latter case, T will be set to the union of the array literal elements.
When creating an empty array you must always specify T:
[] of Int32 # same as Array(Int32).new [] # syntax error
Arrays of strings can be created with a special syntax:
%w(one two three) # ["one", "two", "three"]
Arrays of symbols can be created with a special syntax:
%i(one two three) # [:one, :two, :three]
You can use a special array literal syntax with other types too, as long as they define an argless new
method and a <<
method:
MyType{1, 2, 3}
If MyType
is not generic, the above is equivalent to this:
tmp = MyType.new tmp << 1 tmp << 2 tmp << 3 tmp
If MyType
is generic, the above is equivalent to this:
tmp = MyType(typeof(1, 2, 3)).new tmp << 1 tmp << 2 tmp << 3 tmp
In the case of a generic type, the type arguments can be specified too:
MyType(Int32 | String) {1, 2, "foo"}
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/docs/syntax_and_semantics/literals/array.html