パターンマッチング
対応(29)
Section titled “対応(29)”| 書き方 | 説明 | 注意 |
|---|---|---|
switch (n) { case 1: … } | 文による分岐 | |
case 1: | 文による分岐(数値) | |
n switch { 1 => 10, _ => 0 } | 式による分岐 | int を照合するときは _ => のケースが必要です。 |
m switch { Mode.A => 1, _ => 0 } | 列挙型による分岐 | ケースを尽くしても _ => のケースは省けません。 |
s switch { "a" => 1, _ => 0 } | 文字列による分岐 | |
s is Dot | 型の判定 | |
s is Line l | 型の判定と束縛 | |
case Dot: | case ラベルの型 | union の値を型で分岐する形です。 |
s switch { Dot d => …, Line l => … } | union のケースの網羅 | |
n is 1 | 定数との照合 | |
s is null | null との照合 | |
s is "a" | 文字列の定数との照合 | |
m is Mode.On | 列挙型の定数との照合 | |
n is < 30 | 大小の照合 | |
n is >= 0 and <= 100 | 条件の組み合わせ(and パターン) | |
Next() is >= 0 and <= 100 | 左辺の評価回数 | 条件を 2 つ書いても、Next() の呼び出しは 1 回です。 |
n is 3 or 4 | 条件の組み合わせ(or パターン) | |
s is not null | 否定 | |
n is int and (< 0 or > 100) | 括弧による優先順位 | |
n is var x | 常に成立する束縛 | |
h is { V: 1 } | メンバの値との照合 | 左に置けるのは、自分で宣言した struct と record だけです。 |
h is { V: > 0 } | メンバの値の入れ子の照合 | |
k switch { < 30 => … } | 式のケースの大小 | |
k switch { 3 or 4 => … } | 式のケースの or パターン | |
k switch { int n when n > 3 => … } | 式のケースの条件(when) | |
n switch { var v when v > 0 => … } | 束縛してからの条件 | |
case < 30: | 文のケースの大小 | |
case 3 or 4: | 文のケースの or パターン | |
case int n when n > 3: | 文のケースの条件(when) |
非対応(5)
Section titled “非対応(5)”| 書き方 | 説明 | エラー | 理由 | 代替 |
|---|---|---|---|---|
p is (1, 2) | 分解して位置で照合する | TUKI0001 | 未実装 | メンバの値で照合する({ Item1: 1 }) |
value is int(型引数の値に対して) | 型引数の値にパターンを当てる | TUKI0001 | 未実装 | 型引数を使わない形に書き換える |
s switch { Line l when … => … } | union の型パターンに条件を付ける | TUKI0001 | 未実装 | 条件をケースの本体へ移すか、when を使わずケースを分ける |
n switch { 1 => 10, 2 => 20 } | 既定のケースを省く(int) | TUKI0001 | 設計 | _ => を置く。union に対してだけ省ける |
m switch { Mode.A => 1, Mode.B => 2 } | 既定のケースを省く(列挙型) | TUKI0001 | 設計 | ケースを尽くしても省けない |
| 既定のケースを省ける形 | union のときだけです。int と列挙型では、ケースを尽くしても _ => のケースが必要です |
when を付けられない形 | union の型パターンのケースだけです。値のパターンと関係パターンには、switch の文でも式でも付けられます |
定数による分岐
Section titled “定数による分岐”文による分岐
Section titled “文による分岐”using UnityEngine;using Tsukimi;
public class PatSwitchStatement : TsukimiBehaviour{ void Start() { int n = 1; switch (n) { case 1: Debug.Log(10); break; // => 10 default: Debug.Log(0); break; } }}文による分岐(数値)
Section titled “文による分岐(数値)”using UnityEngine;using Tsukimi;
public class IsoSwStmtConst : TsukimiBehaviour{ void Start() { int k = 1; switch (k) { case 1: Debug.Log(1); break; default: Debug.Log(0); break; } // => 1 }}式による分岐
Section titled “式による分岐”switch の式は、一致したケースの値を結果にします。
using UnityEngine;using Tsukimi;
public class PatDiscardArm : TsukimiBehaviour{ void Start() { int n = 1; int r = n switch { 1 => 10, _ => 0 }; Debug.Log(r); // => 10 }}列挙型による分岐
Section titled “列挙型による分岐”列挙型の定数をケースに書けます。
using UnityEngine;using Tsukimi;
public enum Mode { A, B }
public class PatEnumConstant : TsukimiBehaviour{ void Start() { Mode m = Mode.A; int r = m switch { Mode.A => 1, _ => 0 }; Debug.Log(r); // => 1 }}文字列による分岐
Section titled “文字列による分岐”using UnityEngine;using Tsukimi;
public class PatStringConstant : TsukimiBehaviour{ void Start() { string s = "a"; int r = s switch { "a" => 1, _ => 0 }; Debug.Log(r); // => 1 }}型による振り分け
Section titled “型による振り分け”is の右に型を書く形は、union とインタフェースの値に対してだけ使えます。
using UnityEngine;using Tsukimi;
public abstract record Shape;public record Dot : Shape;public record Line(int Len) : Shape;
public class IsoIsTypeUnion : TsukimiBehaviour{ void Start() { Shape s = new Dot(); bool b = s is Dot; Debug.Log(b); // => true }}型の判定と束縛
Section titled “型の判定と束縛”using UnityEngine;using Tsukimi;
public abstract record Shape;public record Dot : Shape;public record Line(int Len) : Shape;
public class PtDeclaration : TsukimiBehaviour{ void Start() { Shape s = new Line(3); if (s is Line l) { Debug.Log(l.Len); } // => 3 }}case ラベルの型
Section titled “case ラベルの型”case ラベルにも型を書けます。
using UnityEngine;using Tsukimi;
public abstract record Shape;public record Dot : Shape;public record Line(int Len) : Shape;
public class PtTypeCase : TsukimiBehaviour{ void Start() { Shape s = new Dot(); switch (s) { case Dot: Debug.Log(0); break; default: Debug.Log(1); break; } // => 0 }}union のケースの網羅
Section titled “union のケースの網羅”union のケースをすべて書けば、_ => のケースを省けます。
using UnityEngine;using Tsukimi;
public abstract record Shape;public record Dot : Shape;public record Line(int Len) : Shape;
public class PtExhaustiveUnion : TsukimiBehaviour{ void Start() { Shape s = new Dot(); int v = s switch { Dot d => 0, Line l => l.Len }; Debug.Log(v); // => 0 }}値の条件での照合
Section titled “値の条件での照合”定数との照合
Section titled “定数との照合”using UnityEngine;using Tsukimi;
public class IsoIsConst : TsukimiBehaviour{ void Start() { int k = 1; bool b = k is 1; Debug.Log(b); // => true }}null との照合
Section titled “null との照合”using UnityEngine;using Tsukimi;
public class PtConstNull : TsukimiBehaviour{ void Start() { string s = null; if (s is null) { Debug.Log(1); } // => 1 }}文字列の定数との照合
Section titled “文字列の定数との照合”using UnityEngine;using Tsukimi;
public class PtConstString : TsukimiBehaviour{ void Start() { string s = "a"; if (s is "a") { Debug.Log(1); } // => 1 }}列挙型の定数との照合
Section titled “列挙型の定数との照合”using UnityEngine;using Tsukimi;
public enum Mode { Off, On }public class PtConstEnum : TsukimiBehaviour{ void Start() { Mode m = Mode.On; if (m is Mode.On) { Debug.Log(1); } // => 1 }}using UnityEngine;using Tsukimi;
public class PtRelational : TsukimiBehaviour{ void Start() { int k = 1; if (k is < 30) { Debug.Log(1); } // => 1 }}条件の組み合わせ(and パターン)
Section titled “条件の組み合わせ(and パターン)”using UnityEngine;using Tsukimi;
public class PtAnd : TsukimiBehaviour{ void Start() { int k = 1; if (k is >= 0 and <= 100) { Debug.Log(1); } // => 1 }}左辺の評価回数
Section titled “左辺の評価回数”is の左は 1 度しか評価されません。
using UnityEngine;using Tsukimi;
public class PtAndOnce : TsukimiBehaviour{ int Next() { Debug.Log(9); // => 9 return 1; }
void Start() { if (Next() is >= 0 and <= 100) { Debug.Log(1); } // => 1 }}条件の組み合わせ(or パターン)
Section titled “条件の組み合わせ(or パターン)”using UnityEngine;using Tsukimi;
public class PtOr : TsukimiBehaviour{ void Start() { int k = 3; if (k is 3 or 4) { Debug.Log(1); } // => 1 }}using UnityEngine;using Tsukimi;
public class PtNot : TsukimiBehaviour{ void Start() { string s = "a"; if (s is not null) { Debug.Log(1); } // => 1 }}括弧による優先順位
Section titled “括弧による優先順位”using UnityEngine;using Tsukimi;
public class PtParen : TsukimiBehaviour{ void Start() { int k = 1; if (k is int and (< 0 or > 100)) { Debug.Log(1); } }}常に成立する束縛
Section titled “常に成立する束縛”using UnityEngine;using Tsukimi;
public class PtVar : TsukimiBehaviour{ void Start() { int k = 1; if (k is var x) { Debug.Log(x); } // => 1 }}メンバの値との照合
Section titled “メンバの値との照合”{ } の中に、メンバの名前と照合する値を書きます。
using UnityEngine;using Tsukimi;
public struct Holder { public int V { get; set; } }public class PtProperty : TsukimiBehaviour{ void Start() { Holder h = new Holder(); if (h is { V: 1 }) { Debug.Log(1); } }}メンバの値の入れ子の照合
Section titled “メンバの値の入れ子の照合”using UnityEngine;using Tsukimi;
public struct Holder { public int V { get; set; } }public class PtNested : TsukimiBehaviour{ void Start() { Holder h = new Holder(); if (h is { V: > 0 }) { Debug.Log(1); } }}switch のケースの条件
Section titled “switch のケースの条件”式のケースの大小
Section titled “式のケースの大小”using UnityEngine;using Tsukimi;
public class IsoSwExprRel : TsukimiBehaviour{ void Start() { int k = 1; int v = k switch { < 30 => 1, _ => 0 }; Debug.Log(v); // => 1 }}式のケースの or パターン
Section titled “式のケースの or パターン”using UnityEngine;using Tsukimi;
public class IsoSwExprOr : TsukimiBehaviour{ void Start() { int k = 3; int v = k switch { 3 or 4 => 1, _ => 0 }; Debug.Log(v); // => 1 }}式のケースの条件(when)
Section titled “式のケースの条件(when)”using UnityEngine;using Tsukimi;
public class IsoSwExprWhen : TsukimiBehaviour{ void Start() { int k = 5; int v = k switch { int n when n > 3 => n, _ => 0 }; Debug.Log(v); // => 5 }}束縛してからの条件
Section titled “束縛してからの条件”using UnityEngine;using Tsukimi;
public class R_wc : TsukimiBehaviour{ void Start() { int n = 1; int r = n switch { var v when v > 0 => 10, _ => 0 }; Debug.Log(r); // => 10 }}文のケースの大小
Section titled “文のケースの大小”using UnityEngine;using Tsukimi;
public class IsoSwStmtRel : TsukimiBehaviour{ void Start() { int k = 12; switch (k) { case < 30: Debug.Log(1); break; default: Debug.Log(0); break; } // => 1 }}文のケースの or パターン
Section titled “文のケースの or パターン”using UnityEngine;using Tsukimi;
public class IsoSwStmtOr : TsukimiBehaviour{ void Start() { int k = 4; switch (k) { case 3 or 4: Debug.Log(1); break; default: Debug.Log(0); break; } // => 1 }}文のケースの条件(when)
Section titled “文のケースの条件(when)”ケースに書ける照合の構文は、switch の文と式で同じです。
using UnityEngine;using Tsukimi;
public class IsoSwStmtWhen : TsukimiBehaviour{ void Start() { int k = 5; switch (k) { case int n when n > 3: Debug.Log(n); break; default: Debug.Log(0); break; } // => 5 }}