Tuples

Lists are good for when we want a collection of one type, but sometimes we want to combine multiple values of different types. In this case tuples are a quick and convenient option.

リストは 1 つの型のコレクションが欲しい場合には適していますが、異なる型の複数の値を組み合わせたい場合もあります。このような場合、タプルが手っ取り早く便利な選択肢となります。

The tuple access syntax can be used to get elements from a tuple without pattern matching. some_tuple.0 gets the first element, some_tuple.1 gets the second element, etc.

タプルのアクセス構文を使用すると、パターンマッチなしでタプルから要素を取り出すことができます。some_tuple.0 は最初の要素を取得し、some_tuple.1 は 2 番目の要素を取得するといった具合です。

Tuples are generic types, they have type parameters for the types they contain. #(1, "Hi!") has the type #(Int, String), and #(1.4, 10, 48) has the type #(Float, Int, Int).

タプルは一般的な型であり、タプルに含まれる型の型パラメータを持ちます。#(1, "Hi!")#(Int, String) 型となり、#(1.4, 10, 48)#(Float, Int, Int) 型となります。

Tuples are most commonly used to return 2 or 3 values from a function. Other times it is often is clearer to use a custom type, which we will cover next.

タプルは、関数から 2, 3 個の値を返すときによく使われます。また、ある時はカスタムタイプを使用するほうが明確になる場合がしばしばあります。