Skip to content

Tuple

SyntaxDescriptionNote
(int, int) p = (1, 2);Declaration by position
(int x, int y) p = (1, 2);Element names
((int, int), int) p = ((1, 2), 3);Nested tuples
(int a, int b) = Pair();Deconstructing a return value
(a, b) = (b, a);Deconstructing assignment to declared variables
(xs[i], xs[j]) = (xs[j], xs[i]);Deconstructing assignment to array elements
int Take((int, int) p)Receiving as a parameter
public (int, int) P;Holding as a field
(int, int)[] ps = new (int, int)[1];Array elementElements are read and written as ps[0].Item1.
a == bTuple equality
SyntaxDescriptionErrorReasonAlternative
(long, long) b = a;Convert between tuples element by elementTUKI0099not yetMove the elements one at a time (conversions)
((int, int))aExplicitly convert a tupleTUKI0099not yetSame as above
(int, int)[][] ps = new (int, int)[2][];Creating a jagged array of tuplesTUKI0001not yetUse a 1D array, or hold a separate array per element
(a, b) == (c, d) (tuples of different element types)Comparing tuples of different element typesTUKI0099not yetConvert each element before comparing
How the value is heldIt travels as a value, like a struct
Where it can liveLocal variables, parameters, return values, fields, array elements

Elements declared without names are read as Item1, Item2, and so on.

using UnityEngine;
using Tsukimi;
public class TyTupleDecl : TsukimiBehaviour
{
void Start()
{
(int, int) p = (1, 2);
Debug.Log(p.Item1); // => 1
}
}
using UnityEngine;
using Tsukimi;
public class TyTupleNamed : TsukimiBehaviour
{
void Start()
{
(int x, int y) p = (1, 2);
Debug.Log(p.x); // => 1
}
}
using UnityEngine;
using Tsukimi;
public class TyTupleNested : TsukimiBehaviour
{
void Start()
{
((int, int), int) p = ((1, 2), 3);
Debug.Log(p.Item2); // => 3
}
}
using UnityEngine;
using Tsukimi;
public class TyTupleReturn : TsukimiBehaviour
{
void Start()
{
(int a, int b) = Pair();
Debug.Log(a + b); // => 3
}
private (int, int) Pair() { return (1, 2); }
}

Deconstructing assignment to declared variables

Section titled “Deconstructing assignment to declared variables”
using UnityEngine;
using Tsukimi;
public class TupleAssignExisting : TsukimiBehaviour
{
void Start()
{
int a = 1;
int b = 2;
(a, b) = (b, a);
Debug.Log(a * 10 + b); // => 21
}
}

Deconstructing assignment to array elements

Section titled “Deconstructing assignment to array elements”

The assignment target can also be an array element.

using UnityEngine;
using Tsukimi;
public class TupleAssignArrayElements : TsukimiBehaviour
{
void Start()
{
int[] xs = new int[2];
xs[0] = 1;
xs[1] = 2;
int i = 0;
int j = 1;
(xs[i], xs[j]) = (xs[j], xs[i]);
Debug.Log(xs[0] * 10 + xs[1]); // => 21
}
}
using UnityEngine;
using Tsukimi;
public class TyTupleParam : TsukimiBehaviour
{
void Start()
{
Debug.Log(Take((1, 2))); // => 1
}
private int Take((int, int) p) { return p.Item1; }
}
using UnityEngine;
using Tsukimi;
public class TyTupleField : TsukimiBehaviour
{
void Start()
{
P = (1, 2);
Debug.Log(P.Item1); // => 1
}
public (int, int) P;
}

It can be an array element.

using UnityEngine;
using Tsukimi;
public class TyTupleArray : TsukimiBehaviour
{
void Start()
{
(int, int)[] ps = new (int, int)[1];
ps[0] = (1, 2);
Debug.Log(ps[0].Item1); // => 1
}
}
using UnityEngine;
using Tsukimi;
public class ExpressionsTupleEquality : TsukimiBehaviour
{
void Start()
{
(int, int) a = (1, 2);
(int, int) b = (1, 2);
Debug.Log(a == b); // => true
}
}