# Making "match" catch exceptions

- Thread: https://staging.externals.io/message/113804
- Original archive: https://news-web.php.net/php.internals/113804
- Started by olleharstedt@gmail.com on 2021-03-26T16:45:58+00:00
- Messages: 5
- RFC: Match expression v2 (Implemented (PHP 8.0)) — https://staging.externals.io/rfc/match_expression_v2.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://staging.externals.io/search.md?q={query} · RFCs at https://staging.externals.io/rfc.md · JSON API at https://staging.externals.io/api · documentation at https://staging.externals.io/llms.txt

---

## #113804 — olleharstedt@gmail.com — 2021-03-26T16:45:58+00:00

URL: https://staging.externals.io/message/113804#113804

Hi,

Someone mentioned that they prefer lambdas over try-catch blocks,
which surprised me, since try-catch is completely idiomatic in both
PHP and OCaml (Haskell I don't know). That made me think of a recent
extension to OCaml which makes it possible to catch exceptions in a
match-expressions (here's one blog post about it:
https://blog.janestreet.com/pattern-matching-and-exception-handling-unite/).

Now when PHP also has `match`, are there any plans to extend it with
exception catching? So far I did not find anything about exceptions in
the match RFC v2: https://wiki.php.net/rfc/match_expression_v2

Kind regards
Olle

---

## #113806 — Rowan Tommins — 2021-03-26T22:00:28+00:00

URL: https://staging.externals.io/message/113804#113806 · In reply to: #113804

> That made me think of a recent
> extension to OCaml which makes it possible to catch exceptions in a
> match-expressions (here's one blog post about it:
> https://blog.janestreet.com/pattern-matching-and-exception-handling-unite/).


That's an interesting idea. I wonder if it would provide a way to 
implement the "data-flow" or "expression context" exception handling I 
was discussing on a different thread recently.

Looking at my previous example:

try {
    $foo = doSomething() + $bar;
}
catch ( SomeException $e ) {
    $foo = $bar;
}

Would become something like this:

$foo = match($temp = doSomething()) {
    exception SomeException => 0,
    default => $temp
} + $bar;


Hm, that's not the most elegant; even if we could avoid the $temp 
assignment, the "default to myself" seems superfluous for this use case.

I can definitely see the feature having its use cases, though, was just 
kicking this around as a thought experiment.

Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #113807 — olleharstedt@gmail.com — 2021-03-26T22:58:09+00:00

URL: https://staging.externals.io/message/113804#113807 · In reply to: #113806

2021-03-26 23:00 GMT+01:00, Rowan Tommins <rowan.collins@gmail.com>:
> 
> > […]
>
>
> That's an interesting idea. I wonder if it would provide a way to
> implement the "data-flow" or "expression context" exception handling I
> was discussing on a different thread recently.
>
> Looking at my previous example:
>
> try {
>     $foo = doSomething() + $bar;
> }
> catch ( SomeException $e ) {
>     $foo = $bar;
> }
>
> Would become something like this:
>
> $foo = match($temp = doSomething()) {
>     exception SomeException => 0,
>     default => $temp
> } + $bar;

You can't catch the value of $temp in the match-case?

    $foo = $bar + match (doSomething()) {
      $temp => $temp,
      exception SomeException => 0
    };

Olle

---

## #113810 — Rowan Tommins — 2021-03-27T14:30:16+00:00

URL: https://staging.externals.io/message/113804#113810 · In reply to: #113807

> 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
};


Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #113811 — olleharstedt@gmail.com — 2021-03-27T15:14:45+00:00

URL: https://staging.externals.io/message/113804#113811 · In reply to: #113810

2021-03-27 15:30 GMT+01:00, Rowan Tommins <rowan.collins@gmail.com>:
> 
> > […]
>
>
> $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
