= "Oh no!"; break; case 8.0: $result = "This is what I expected"; break; } echo $result; //> Oh no! // PHP8 echo match (8.0) { '8.0' => "Oh no!", 8.0 => "This is what I expected", }; //> This is what I expected
"Oh no!"; break; case 8.0: // $result = "This is what I expected"; break; } echo $result; //> Oh no! // PHP8 echo match ($x) { '8.0' => "Oh no!", 8.0 => "This is what I expected", }; //> This is what I expected • switch文だと代入忘れが起こりえるがmatch式だと起こり得ない $resultへの代入を忘 れてもとりあえず動く マッチ後の結果値を 記述しないと実行時 にParseErrorが出る
$result = "Oh no!\n"; break; case 5: $result = "This is what I expected\n"; break; } echo $result; // >Oh no! // PHP8 echo match (5) { '5' => "Oh no!\n", 5 => "This is what I expected\n" } // >This is what I expected intの5がstringの”5”にマッチしてしまう
function on null”エラーが発生する。 b. Facebook製のHackというPHPの姉妹言語がこの実装方針を採用している c. 実は2014年ごろにもnullsafe operatorのRFCが出ていたが、この実装方針 (おそらくHackの影 響)で行こうとしてrejectされた。 2. 関数の引数に対してのみShort Circuitingを使うNullsafe operator a. foo()とbaz()は評価されるが、bar()は(foo()の引数なので)評価されない。foo()の評価はnullにな り、nullに対してbaz()を呼ぶので”Call to a member function on null”エラーが発生する。 b. RubyやKotlinなど複数の言語がこの実装方針を採用している Short Circuitingについての実装方針 null?->foo(bar())->baz();
Hearts; case Diamonds; case Clubs; case Spades; } function pick_a_card(Suit $suit) { ... } pick_a_card(Suit::Clubs); // OK pick_a_card('Clubs'); // TypeError: pick_a_card(): Argument #1 ($suit) must be of type Suit, string given SuitはHearts, Diamonds, Clubs, Spadesのどれか 内部的にはこれらの実体はオブ ジェクト pick_a_card関数はSuit::Hearts, Suit::Diamonds, Suit::Clubs, Suit::Spadesのいずれかのみを引数として 受け付ける
Diamonds; case Clubs; case Spades; public function shape(): string { return "Rectangle"; } } print Suit::Diamonds->shape(); // prints "rectangle" Hearts, Diamonds, Clubs, Spades はshape()という関数を持つ
case Kilometers(public int $km); case Miles(public int $miles); } $my_walk = Distance::Miles(500); print $my_walk->miles; // prints "500" $my_walk2 = Distance::Kilometers(500); print $my_walk2->km; // prints "500"
public function bind(callable $f) { return $this; } }; // This is a Tagged Case. case Some(private mixed $value) { public function bind(callable $f): Maybe { return $f($this->value); } }; public function value(): mixed { if ($this instanceof None) { throw new Exception(); } return $this->val; } } Someの引数として任意の型の値 を与えることができる。 Someのプロパティとして値が紐付 けられる。 前述のconstructor property promotionを使っている。
is_string($foo) $foo is int|float; // Equivalent to is_int($foo) || is_float($foo) $foo is Request; // Equivalent to $foo instanceof Request $foo is User|int; // Equivalent to $foo instanceof User || is_int($foo) $foo is ?array; // Equivalent to is_array($foo) || is_null($foo)
__construct(public int $x, public int $y, public int $z) {} } $p = new Point(3, 4, 5); $p is Point {x: 3}; // Equivalent to: $p instanceof Point && $p->x === 3; $p is Point {y: 37, x: 2,}; // Equivalent to: $p instanceof Point && $p->y === 37 && $p->x === 2;
= match ($p) is { Point{x: 3, y: 9, %$z} => "x is 3, y is 9, z is $z", Point{%$z, %$x, y: 4} => "x is $x, y is 4, z is $z", Point{x: 5, %$y} => "x is 5, y is $y, and z doesn't matter", // This will always match. Point{%$x, %$y, %$z} => "x is $x, y is $y, z is $z", };