Variables and passing arguments
Forms that compile (13)
Section titled “Forms that compile (13)”| Syntax | Description |
|---|---|
int a = 1; | Local variable |
var a = 1; | Type inference |
int a = 1, b = 2; | Combined declaration |
{ int a = 1; } | Scope from a block |
int a; a = 1; | Assign after declaring |
private static int Counter; | Static variable |
int Take(int v) | Pass by value |
int Read(in int v) | in parameter |
int.TryParse("1", out n) | out from a method on the runtime |
out int n | Declare an out variable inline |
if (…) a = 1; else a = 2; | Assignment in if and else |
switch … case … default … | Assignment in every switch case |
_ = Make(); | Discard a return value |
Forms that don’t compile (9)
Section titled “Forms that don’t compile (9)”| Syntax | Description | Error | Reason | Instead |
|---|---|---|---|---|
Bump(ref a) | Pass ref to a method you wrote | TUKI0001 | not yet | Return it as a value |
Make(out int a) | Receive out from a method you wrote | TUKI0001 | not yet | Return it as a value |
Bump(ref a[0]) | Pass an array element by ref | TUKI0001 | not yet | Pass the value and write it back through the return value |
ref int r = ref a; | A local that points to the variable itself | TUKI0001 | undecided | Hold a value |
ref int First(int[] xs) | Return the variable itself | TUKI0001 | undecided | Return a value and write it back at the call site |
r = ref b; | Replace what it points to | TUKI0001 | undecided | Same as above |
ref (c ? ref a : ref b) | Choose what it points to by a condition | TUKI0001 | undecided | Same as above |
Make(out _); | Discard the out destination | TUKI0001, TUKI0099 | not yet | Receive it with a variable |
int.TryParse("1", out _) | Discard the out of a method on the runtime | TUKI0099 | not yet | Receive it with a variable |
Forms that hold a variable’s location itself as a value don’t compile. A ref local, a ref return, ref reassignment, and ref in a conditional expression all produce TUKI0001. Return the changed result as a value and assign it at the call site. |
Definite assignment follows the same rules as C#. If every branch assigns it, it can be read afterward. Reading before assignment produces CS0165.
Declaration
Section titled “Declaration”Local variable
Section titled “Local variable”using UnityEngine;using Tsukimi;
public class VarLocal : TsukimiBehaviour{ void Start() { int a = 1; Debug.Log(a); // => 1 }}Type inference
Section titled “Type inference”using UnityEngine;using Tsukimi;
public class VarInferredType : TsukimiBehaviour{ void Start() { var a = 1; Debug.Log(a); // => 1 }}Combined declaration
Section titled “Combined declaration”using UnityEngine;using Tsukimi;
public class VarMultipleDeclarators : TsukimiBehaviour{ void Start() { int a = 1, b = 2; Debug.Log(a + b); // => 3 }}Scope from a block
Section titled “Scope from a block”using UnityEngine;using Tsukimi;
public class VarBlockScope : TsukimiBehaviour{ void Start() { { int a = 1; Debug.Log(a); // => 1 } }}Assign after declaring
Section titled “Assign after declaring”You can declare it alone and assign later. Reading before assignment is rejected with CS0165.
using UnityEngine;using Tsukimi;
public class VarAssignBeforeUse : TsukimiBehaviour{ void Start() { int a; a = 1; Debug.Log(a); // => 1 }}Static variable
Section titled “Static variable”It belongs to the type, not the instance. The value is separate per behaviour instance, and isn’t shared between instances.
using UnityEngine;using Tsukimi;
public class VaStatic : TsukimiBehaviour{ void Start() { Counter = Counter + 1; Debug.Log(Counter); // => 1 }
private static int Counter;}Arguments
Section titled “Arguments”Pass by value
Section titled “Pass by value”A copy of the value is passed to the parameter. Assigning to the parameter in the callee doesn’t change the caller’s variable.
using UnityEngine;using Tsukimi;
public class VaValueParam : TsukimiBehaviour{ void Start() { Debug.Log(Take(1)); // => 1 }
private int Take(int v) { return v; }}in parameter
Section titled “in parameter”The declaration can state that the callee won’t assign to the parameter. Unlike ref and out, this can also be written on a method you wrote.
using UnityEngine;using Tsukimi;
public class VarInParameter : TsukimiBehaviour{ private int Read(in int v) { return v; }
void Start() { int a = 1; Debug.Log(Read(in a)); // => 1 }}out from a method on the runtime
Section titled “out from a method on the runtime”out can only be written when calling a method on the runtime. It can’t be written on a method you wrote (TUKI0001).
using UnityEngine;using Tsukimi;
public class VarOutFromExtern : TsukimiBehaviour{ void Start() { int n; bool ok = int.TryParse("1", out n); Debug.Log(ok ? n : 0); // => 1 }}Declare an out variable inline
Section titled “Declare an out variable inline”using UnityEngine;using Tsukimi;
public class VarOutDeclaredInline : TsukimiBehaviour{ void Start() { bool ok = int.TryParse("1", out int n); Debug.Log(ok ? n : 0); // => 1 }}Definite assignment
Section titled “Definite assignment”Assignment in if and else
Section titled “Assignment in if and else”using UnityEngine;using Tsukimi;
public class VaDefAssignIf : TsukimiBehaviour{ void Start() { int a; if (true) { a = 1; } else { a = 2; } Debug.Log(a); // => 1 }}Assignment in every switch case
Section titled “Assignment in every switch case”using UnityEngine;using Tsukimi;
public class VaDefAssignSwitch : TsukimiBehaviour{ void Start() { int k = 1; int a; switch (k) { case 1: a = 1; break; default: a = 0; break; } Debug.Log(a); // => 1 }}Discard
Section titled “Discard”Discard a return value
Section titled “Discard a return value”using UnityEngine;using Tsukimi;
public class VarDiscard : TsukimiBehaviour{ private int calls;
void Start() { _ = Make(); Debug.Log(calls); // => 1 }
private int Make() { calls = calls + 1; return 1; }}