タプル

2026年3月22日
1 分

タプルのリテラルは、(1, 2, 3) のように ( ) で値をくくったものだ。 Tuple 型のオブジェクトで、任意の数の型パラメータを持つ。

julia> t = (1, 2, 3)
(1, 2, 3)

julia> typeof(t)
Tuple{Int64, Int64, Int64}

タプルの各要素には、1 から始まるインデックスでアクセスできる。

julia> t[1]
1

julia> t[2]
2

タプルは immutable なので、要素の更新などはできない。

julia> t[3] = 10
ERROR: MethodError: no method matching setindex!(::Tuple{Int64, Int64, Int64}, ::Int64, ::Int64)
The function `setindex!` exists, but no method is defined for this combination of argument types.
Stacktrace:
 [1] top-level scope
   @ REPL[5]:1

関数の可変長引数も、実はタプル。

julia> f(x...) = x
f (generic function with 1 method)

julia> y = f(1, 2, 3)
(1, 2, 3)

julia> typeof(y)
Tuple{Int64, Int64, Int64}