AllIsHackedOff

Just a memo, just a progress

Crystalの配列(0)

配列

[1,2,3] [1, "aaa", 'ks']

文字列配列はRubyと同じ表記が可能

%w(one two three)

シンボルの配列

%i(one two three)

配列ライクな型

https://crystal-lang.org/api/0.20.3/Array.html

You can use a special array literal syntax with other types too, as long as they define an argless .new method and a #<< method. Set is one such type:

set = Set(typeof(1, 2, 3)).new
set << 1
set << 2

引数なしのnewと << が定義されていれば Array特有の文法を使うことができる。

Arrayはgenericな型、複数の型を含む配列の方はUNION型と呼ばれる [1, "aaa", 'ks']など

structural subtyping的になっているので、呼び出されるメソッドに応答するればその型の 条件を満たしているとみなされ、コンパイルが通る

=> 不用意に型制約をすると柔軟性が減る