Tuple
Forms that compile (10)
Section titled “Forms that compile (10)”| Syntax | Description |
|---|---|
(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 == b | Tuple equality |
Forms that don’t compile (2)
Section titled “Forms that don’t compile (2)”| Syntax | Description | Error | Reason | Instead |
|---|---|---|---|---|
(long, long) b = a; | Assigning one tuple to another | TUKI0099 | not yet | Move the elements one at a time (conversions) |
(int, int)[][] ps = new (int, int)[2][]; | Creating a jagged array of tuples | TUKI0001 | not yet | Use 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. |
Generation
Section titled “Generation”Declaration by position
Section titled “Declaration by position”using UnityEngine;using Tsukimi;
public class TyTupleDecl : TsukimiBehaviour{ void Start() { (int, int) p = (1, 2); Debug.Log(p.Item1); // => 1 }}Element names
Section titled “Element names”using UnityEngine;using Tsukimi;
public class TyTupleNamed : TsukimiBehaviour{ void Start() { (int x, int y) p = (1, 2); Debug.Log(p.x); // => 1 }}Nested tuples
Section titled “Nested tuples”using UnityEngine;using Tsukimi;
public class TyTupleNested : TsukimiBehaviour{ void Start() { ((int, int), int) p = ((1, 2), 3); Debug.Log(p.Item2); // => 3 }}Passing
Section titled “Passing”Deconstructing a return value
Section titled “Deconstructing a return value”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 }}Receiving as a parameter
Section titled “Receiving as a parameter”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; }}Holding as a field
Section titled “Holding as a field”using UnityEngine;using Tsukimi;
public class TyTupleField : TsukimiBehaviour{ void Start() { P = (1, 2); Debug.Log(P.Item1); // => 1 }
public (int, int) P;}Array element
Section titled “Array element”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 }}Tuple equality
Section titled “Tuple equality”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 }}