コンテンツにスキップ

タプル

タプルは通常、タプルリテラルで作成されます。

tuple = {1, "hello", 'x'} # Tuple(Int32, String, Char)
tuple[0]                  # => 1       (Int32)
tuple[1]                  # => "hello" (String)
tuple[2]                  # => 'x'     (Char)

空のタプルを作成するには、Tuple.newを使用します。

タプル型を示すには、次のように記述できます。

# The type denoting a tuple of Int32, String and Char
Tuple(Int32, String, Char)

型制約、ジェネリック型の引数、および型が期待されるその他の場所では、型文法で説明されているように、より短い構文を使用できます。

# An array of tuples of Int32, String and Char
Array({Int32, String, Char})

スプラット展開

スプラット演算子は、タプルリテラル内で一度に複数の値を展開するために使用できます。スプラットされる値は別のタプルである必要があります。

tuple = {1, *{"hello", 'x'}, 2} # => {1, "hello", 'x', 2}
typeof(tuple)                   # => Tuple(Int32, String, Char, Int32)

tuple = {3.5, true}
tuple = {*tuple, *tuple} # => {3.5, true, 3.5, true}
typeof(tuple)            # => Tuple(Float64, Bool, Float64, Bool)