Tuples​

A tuple type is a heterogeneous sequence of other types. Tuples can be either named or unnamed (the default).

Constructing tuples​

A tuple constructor is an expression that consists of a sequence of comma-separated expressions enclosed in parentheses. It produces a tuple value:


1. "(" expr [, ... ] ")"

Declare a named tuple:


1. "(" identifier := expr [, ... ] ")"

All elements in a named tuple must have a name.

A tuple constructor automatically creates a corresponding tuple type.

Accessing elements​

An element of a tuple can be referenced in the form:


1. expr.element-index

Here, expr is any expression that has a tuple type, and element-index is either the zero-based index of the element or the name of an element in a named tuple.

Examples:


1. select (1, 'EdgeDB').0;


1. {1}


1. select (number := 1, name := 'EdgeDB').name;


1. {"EdgeDB"}


1. select (number := 1, name := 'EdgeDB').1;


1. {"EdgeDB"}

Nesting tuples​

Tuples can be nested:


1. select (nested_tuple := (1, 2)).nested_tuple.0;


1. {1}

Referencing a non-existent tuple element will result in an error:


1. select (1, 2).5;


1. EdgeQLError: 5 is not a member of a tuple

3. ---- query context ----

5. line 1
6. > select (1, 2).3;

Type syntax​

A tuple type can be explicitly declared in an expression or schema declaration using the following syntax:


1. tuple "<" element-type, [element-type, ...] ">"

A named tuple:


1. tuple "<" element-name : element-type [, ... ] ">"

Any type can be used as a tuple element type.

Here’s an example of using this syntax in a schema definition:


1. type GameElement {
2. required property name -> str;
3. required property position -> tuple<x: int64, y: int64>;
4. }

Here’s a few examples of using tuple types in EdgeQL queries:


1. select <tuple<int64, str>>('1', 3);


1. {(1, '3')}


1. select <tuple<x: int64, y: int64>>(1, 2);


1. {(x := 1, y := 2)}


1. select (1, '3') is (tuple<int64, str>);


1. {true}


1. select ([1, 2], 'a') is (tuple<array<int64>, str>);


1. {true}

type

tuple

Tuples - 图1

A tuple type is a heterogeneous sequence of other types.

Tuple elements can optionally have names, in which case the tuple is called a named tuple.

Any type can be used as a tuple element type.

A tuple type is created implicitly when a tuple constructor is used:


1. select ('foo', 42);


1. {('foo', 42)}

Two tuples are equal if all of their elements are equal and in the same order. Note that element names in named tuples are not significant for comparison:


1. select (1, 2, 3) = (a := 1, b := 2, c := 3);


1. {true}

The syntax of a tuple type declaration can be found in this section.