2021-03-27 15:30 GMT+01:00, Rowan Tommins <rowan.coll...@gmail.com>: > On 26/03/2021 22:58, Olle Härstedt wrote: >> You can't catch the value of $temp in the match-case? >> >> $foo = $bar + match (doSomething()) { >> $temp => $temp, >> exception SomeException => 0 >> }; > > > $temp doesn't have an outside value, it was an inline assignment to > capture the value of doSomething(); essentially, I wanted this, but > without executing doSomething() twice: > > $foo = $bar + match (doSomething()) { > default => doSomething(), > exception SomeException => 0 > };
Yeah, I got it. Maybe I didn't read enough about the match-expression implementation in PHP, but in OCaml it's possible to capture whatever value the match evaluated to, like: let number_as_string = match number with | 1 -> "one" | v -> "number is " ^ string_of_int v ("^" is string concatenation.) The point of using a variable name like "v" (could be anything) instead of "default" is that like in your example, you don't have to execute the method twice. You can also add guard clauses, like "v when v > 10 -> ...". If you don't plan to use the value, you can use underscore, "_", which I guess would be the corresponding functionality to "default" in the PHP implementation. Olle -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php