tech

[English] [Japanese]

04.3 Tuples

This section describes how to handle tuples, which are combinations of values.

Tuples are available since v1.5.3.

tuple constructor

A tuple creates an object by writing:

// @lnsFront: ok
let obj = (= 1, "abc" ); // 1 "abc"

This is a tuple that holds pairs of values 1, "abc".

A tuple encloses values in (= ).

Must always specify at least one data

Tuple type declaration

The above processing omits the type declaration by type inference, but if you do not omit it, write as follows.

// @lnsFront: ok
let obj:(int,str) = (= 1, "abc" ); // 1 "abc"

(int,str) declares a tuple type whose elements are int and str.

Name of each element of tuple type

When declaring a tuple type, it is possible to attach a symbol to the type of each element as follows:

// @lnsFront: skip
fn func(): (id:int, mess:str);
  • The symbol is id and the type is int.
  • The symbol is mess and the type is str.

However, the above is only to help people understand what the type is, and there is no difference in processing from omitting it as follows.

// @lnsFront: skip
fn func(): (int,str);

unpacking tuple values

Use the ... operator to expand values stored in tuple types.

  // @lnsFront: ok
  fn func(): (int,str) {
     return (= 1, "abc" );
  }

  let val1,val2 = func()...; // 1 "abc"
  print( val1 + 10, val2 .. "xyz" ); // 11 abcxyz

The tuple is expanded by specifying ... after the tuple type value.

Note that it is not possible to refer to each element of a tuple using the following method.

// @lnsFront: error
let val = (= 1, "abc" ); // 1 "abc"
print( val[1] ); // error

that's all.