Skip to content

Tuple

SyntaxDescription
(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 element
a == bTuple equality
SyntaxDescriptionErrorReasonInstead
(long, long) b = a;Assigning one tuple to anotherTUKI0099not yetMove the elements one at a time (conversions)
(int, int)[][] ps = new (int, int)[2][];Creating a jagged array of tuplesTUKI0001not yetUse a single-level array, or hold a separate array per element
A tuple value is carried as a value, like a struct. It can go anywhere: local variables, arguments, return values, fields, array elements.
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 or a field. The index is evaluated only once.

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. Reading and writing an element is written as ps[0].Item1.

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
}
}