# [VOTE] Catching Multiple Exception Types

- Thread: https://externals.io/message/92388
- Original archive: https://news-web.php.net/php.internals/92388
- Started by after89@gmail.com on 2016-04-17T16:51:16+00:00
- Messages: 33
- RFC: Multi catch (Implemented (PHP 7.1)) — https://externals.io/rfc/multiple-catch.md
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/92388.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #92388 — after89@gmail.com — 2016-04-17T16:51:16+00:00

URL: https://externals.io/message/92388#92388

Hi Internals,

We've opened the vote on https://wiki.php.net/rfc/multiple-catch
Voting will end on 2015-05-01

-- 
Regards,
Bronisław Białek.

---

## #92389 — bjorn.x.larsson@telia.com — 2016-04-17T17:35:34+00:00

URL: https://externals.io/message/92388#92389 · In reply to: #92388

Den 2016-04-17 kl. 18:51, skrev Bronisław Białek:
> Hi Internals,
>
> We've opened the vote on https://wiki.php.net/rfc/multiple-catch
> Voting will end on 2015-05-01
>
Very nice that you added "When is it usefull". Could
also have added when is it not useful, but now we are
in voting...

This feature reminds me about Group Use declarations,
adding syntax sugar to improve code quality and make
code updates less error prone.

Regards //Björn Larsson

PS Typo in "usefull", think it's only one l.

---

## #92391 — Dan Ackroyd — 2016-04-17T23:08:53+00:00

URL: https://externals.io/message/92388#92391 · In reply to: #92388

I voted yes.

It's really not that common that I have to do stuff in exception
handling, when I do need to do some clearing up, or checking to see if
the operation that threw the exception needs to be re-tried, having to
repeat that code in all of the catch statements that need it, is just
nasty duplication.

Although it's a small change, having this is a very nice small
improvement that doesn't break anyone's existing code.

Thanks for putting it forward.

cheers
Dan



> Hi Internals,
>
> We've opened the vote on https://wiki.php.net/rfc/multiple-catch
> Voting will end on 2015-05-01
>
> --
> Regards,
> Bronisław Białek.

---

## #92411 — Marco Pivetta — 2016-04-18T13:40:54+00:00

URL: https://externals.io/message/92388#92411 · In reply to: #92391

Heya,

I voted "NO" due to previous discussion. TL;DR: this is FAR off the 80/20
use-case for a language syntax change.

C&P from my answer elsewhere:

The following typical example is something REALLY rare, and requiring a
parser change for it seems excessive:

    try {
        // ...
    } catch (InvalidArgumentException $e) {
        // same handling
    } catch (PDOException $e) {
        // same handling
    } catch (BadMethodCallException $e) {
        // same handling
    }

These 3 exceptions usually result in separate handling anyway. If same
handling is needed, you can as usual extract a private method  (if you are
in a class) and deal with it there:

    try {
        // ...
    } catch (InvalidArgumentException $e) {
        $this->sameHandling($e);
    } catch (PDOException $e) {
        $this->sameHandling($e);
    } catch (BadMethodCallException $e) {
        $this->sameHandling($e);
    }

    private function sameHandling(Throwable $caught)
    {
        // same handling
    }

Still, even in this case, I'd flag it up in a code review, as same handling
for 3 different exception types generally (not always) means something is
really wrong.

So we are building a feature for a 1% case that, while simple to implement
at parser-level in PHP, requires changes in all userland libraries that do
parsing or rely on the AST.

Cheers,

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/



> I voted yes.
>
> It's really not that common that I have to do stuff in exception
> handling, when I do need to do some clearing up, or checking to see if
> the operation that threw the exception needs to be re-tried, having to
> repeat that code in all of the catch statements that need it, is just
> nasty duplication.
>
> Although it's a small change, having this is a very nice small
> improvement that doesn't break anyone's existing code.
>
> Thanks for putting it forward.
>
> cheers
> Dan
>
>
> 
> > […]

---

## #92412 — Zeev Suraski — 2016-04-18T14:05:46+00:00

URL: https://externals.io/message/92388#92412 · In reply to: #92411

> -----Original Message-----
> From: Marco Pivetta [mailto:ocramius@gmail.com]
> Sent: Monday, April 18, 2016 4:41 PM
> To: Dan Ackroyd <danack@basereality.com>
> Cc: Bronisław Białek <after89@gmail.com>; PHP internals
> <internals@lists.php.net>
> Subject: Re: [PHP-DEV] [VOTE] Catching Multiple Exception Types
> 
> Heya,
> 
> I voted "NO" due to previous discussion. TL;DR: this is FAR off the 80/20 use-
> case for a language syntax change.
> 
> C&P from my answer elsewhere:
> 
> The following typical example is something REALLY rare, and requiring a
> parser change for it seems excessive:
> 
>     try {
>         // ...
>     } catch (InvalidArgumentException $e) {
>         // same handling
>     } catch (PDOException $e) {
>         // same handling
>     } catch (BadMethodCallException $e) {
>         // same handling
>     }
> 
> These 3 exceptions usually result in separate handling anyway. If same
> handling is needed, you can as usual extract a private method  (if you are in a
> class) and deal with it there:
> 
>     try {
>         // ...
>     } catch (InvalidArgumentException $e) {
>         $this->sameHandling($e);
>     } catch (PDOException $e) {
>         $this->sameHandling($e);
>     } catch (BadMethodCallException $e) {
>         $this->sameHandling($e);
>     }
> 
>     private function sameHandling(Throwable $caught)
>     {
>         // same handling
>     }
> 
> Still, even in this case, I'd flag it up in a code review, as same handling for 3
> different exception types generally (not always) means something is really
> wrong.

I voted 'yes', as I think that exception handling is one of the few cases where it would make sense to have similar (or completely identical) handling for very different object types.  Emitting a meaningful error and cleaning up are likely to look very similar for potentially quite different exception types.

The main thing that made me question whether it's truly necessary, is that the same can be said about catch (Exception $e), which could handle all exception types in a generic way.  But I think it is not uncommon to have a situation where you'd want to group a certain number of known exception types in one group, and either propagate other exceptions or handle them differently.

I do find this one quite harmless in terms of potential negative implications - so even if it's not exceptionally useful (no pun intended) - it's fairly intuitive, simple in implementation and doesn't have to deal with scalars.  The generic union types RFC has much farther reaching implications in terms of influencing how code is likely to get written in the future, and IMHO not for the better.

Zeev

---

## #92414 — Derick Rethans — 2016-04-18T15:17:39+00:00

URL: https://externals.io/message/92388#92414 · In reply to: #92411

> Heya,
> 
> I voted "NO" due to previous discussion. TL;DR: this is FAR off the 80/20
> use-case for a language syntax change.

I'm with you on this one.

> C&P from my answer elsewhere:
> 
> The following typical example is something REALLY rare, and requiring a
> parser change for it seems excessive:
> 
>     try {
>         // ...
>     } catch (InvalidArgumentException $e) {
>         // same handling
>     } catch (PDOException $e) {
>         // same handling
>     } catch (BadMethodCallException $e) {
>         // same handling
>     }
> 
> These 3 exceptions usually result in separate handling anyway. If same
> handling is needed, you can as usual extract a private method  (if you are
> in a class) and deal with it there:
> 
>     try {
>         // ...
>     } catch (InvalidArgumentException $e) {
>         $this->sameHandling($e);
>     } catch (PDOException $e) {
>         $this->sameHandling($e);
>     } catch (BadMethodCallException $e) {
>         $this->sameHandling($e);
>     }
> 
>     private function sameHandling(Throwable $caught)
>     {
>         // same handling
>     }

Even this is a scary way to do it. As you don't know which exception 
class you have, how would you know how to handle each separately. You'd 
have to if ($caught instanceof InvalidArgumentException) etc to be sure 
that what you're doing with the object is allowed.

If they are user defined exceptions, you should make them implement an 
interface, and do a "catch (IMyException $e)" to be sure you can handle 
the different exceptions in a consistent manor. And that already works.

If they are exception classes defined by other libraries, then, as you 
say, it's very likely they should be handled differenty anyway. Or 
rather, very generically, which you can do with "catch (Exception $e)".

cheers,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug
Posted with an email client that doesn't mangle email: alpine

---

## #92417 — Dan Ackroyd — 2016-04-18T18:11:33+00:00

URL: https://externals.io/message/92388#92417 · In reply to: #92414

> If they are user defined exceptions, you should make them implement an
> interface,

Even within one oranisation, getting different teams to agree to
common interfaces is not always possible. But when the exceptions  are
originating in code that people don't have any control over (e.g.
anything open source) then changing the exceptions to match your
particular interface requirements is a completely impossible.

For example will you and Marco start taking requests to add particular
interfaces to any exceptions thrown by the code in the libraries you
maintain?

Derick wrote:
> As you don't know which exception
> class you have, how would you know how to handle each separately.

Why do you think you need to know how to handle each separately? If
you ever need to do something different based on the type of
exception, you probably wouldn't be using the multiple-catch. The
multiple-catch is almost only ever going to be used when you have a
list of exceptions where you're going to be doing the same thing for
each. e.g. for retrying an operation:

$success = false;
while ($attempts < 4) {
  try {
    foo();
    $success = true;
  }
  catch (NetworkException | DBDeadLockException $e) {
     // Save exceptions to add to surpressed exception
     // when that RFC is hopefully passed.
     $attempts++;
  }
  // All other
}
//Check for success or run out of attempts here.

For NetworkException and DBDeadLockException we're always going to do
the same thing; retry the operation a couple of times. For any
exception type where we would want to do something different......we'd
just use a separate catch block.

cheers
Dan

---

## #92428 — Stanislav Malyshev — 2016-04-18T19:36:36+00:00

URL: https://externals.io/message/92388#92428 · In reply to: #92414

Hi!

> Even this is a scary way to do it. As you don't know which exception 
> class you have, how would you know how to handle each separately. You'd 
> have to if ($caught instanceof InvalidArgumentException) etc to be sure 
> that what you're doing with the object is allowed.

I just recently had a case where I used something similar in Java code,
so I can tell when it's useful:
- You call some complex object that can fail for a lot of reasons (like
HTTP client talking to remote service)
- It can fail with a number of exceptions because something went bad -
i.e. network is bad, DNS is bad, server is down, service is too busy,
your request syntax is wrong, request is correct but you're not allowed
to execute it, etc.
- These causes can be roughly separated into two groups - requests that
we can and should repeat - e.g. "server too busy" situation, and those
that we should not repeat, e.g. "bad syntax" situation
- Furthermore, failures can be "soft" - fail this request but continue
next ones in the batch, or "hard" - drop everything and call for help
because something is seriously broken.

In this situation, grouping exceptions - which, due to being from
different layers of the complex object do not follow any single
structure - becomes very convenient. Of course it is possible to write a
long tree of copy-paste handlers for each of the three cases, but it's
annoying. Much cleaner to have just three branches. And of course I
don't want to do something like catch(Exception) because *shudder*...

> say, it's very likely they should be handled differenty anyway. Or 
> rather, very generically, which you can do with "catch (Exception $e)".

catch(Exception) is practically never a good idea, unless you really are
able to handle *every* exception. Now in PHP that includes parse errors,
type errors, etc. I don't think any random code - except for a very
generic final error handler - would ever want to do that.
-- 
Stas Malyshev
smalyshev@gmail.com

---

## #92433 — Bishop Bettini — 2016-04-19T00:42:58+00:00

URL: https://externals.io/message/92388#92433 · In reply to: #92411

>
> I voted "NO" due to previous discussion. TL;DR: this is FAR off the 80/20
> use-case for a language syntax change.
>
> ...
> These 3 exceptions usually result in separate handling anyway. If same
> handling is needed, you can as usual extract a private method  (if you are
> in a class) and deal with it there:
>
> ...
> Still, even in this case, I'd flag it up in a code review, as same handling
> for 3 different exception types generally (not always) means something is
> really wrong.
>
> So we are building a feature for a 1% case that, while simple to implement
> at parser-level in PHP, requires changes in all userland libraries that do
> parsing or rely on the AST.
>

I agree: the times this need arises are relatively rare, and in those cases
functional encapsulation cleanly handles the repetition. I would vote no,
were it not for one thing: union types
<https://wiki.php.net/rfc/union_types>.

catch constitutes a formal signature, exactly like a function signature. In
my mind, this proposal and the union types proposal are intrinsically tied.
If we have one, we must have the other. If not, we're inducing a sadness.

Since I do see the need for union types, and since I believe the two must
be accepted or rejected together, I voted yes.

---

## #92434 — Stanislav Malyshev — 2016-04-19T00:54:31+00:00

URL: https://externals.io/message/92388#92434 · In reply to: #92433

Hi!

> catch constitutes a formal signature, exactly like a function signature. In
> my mind, this proposal and the union types proposal are intrinsically tied.
> If we have one, we must have the other. If not, we're inducing a sadness.

This has nothing to do with union types. It is just a shortcut for
repeated catch() clauses, it does not introduce anything into type
system, capture existing use case, and doesn't make anything more
complicated. While union types introduce a lot of new things, make
everything dealing with types more complicated and the need for them,
unless we import a real lot of other concepts like type pattern matching
is quite unclear. So I don't think this has much in common.
-- 
Stas Malyshev
smalyshev@gmail.com

---

## #92436 — Bishop Bettini — 2016-04-19T01:09:06+00:00

URL: https://externals.io/message/92388#92436 · In reply to: #92434

On Mon, Apr 18, 2016 at 8:54 PM, Stanislav Malyshev <smalyshev@gmail.com>
wrote:

> Hi!
>
> > […]
> In
> > […]
> tied.
> > […]
>
> This has nothing to do with union types. It is just a shortcut for
> repeated catch() clauses, it does not introduce anything into type
> system, capture existing use case, and doesn't make anything more
> complicated. While union types introduce a lot of new things, make
> everything dealing with types more complicated and the need for them,
> unless we import a real lot of other concepts like type pattern matching
> is quite unclear. So I don't think this has much in common.
>

It's about the perception of consistency. "Oh, I can do this! Neat:"

function neat(Foo | Bar $a) { ... }

"But I can't do this? WTF?"

catch (FooException | BarException $ex) { ... }

And vice-versa. The perception revolves around the fact that both appear to
be signatured, regardless of how they're implemented in the engine.

---

## #92437 — Stanislav Malyshev — 2016-04-19T01:28:15+00:00

URL: https://externals.io/message/92388#92437 · In reply to: #92436

Hi!

> It's about the perception of consistency. "Oh, I can do this! Neat:"
> 
> function neat(Foo | Bar $a) { ... }

You shouldn't be able to do this, because it makes no sense - why would
a function accept two random types and only them? That's probably a bad
design - it should be one type or two functions.

> "But I can't do this? WTF?"
> 
> catch (FooException | BarException $ex) { ... }

But this makes total sense - you routinely catch more than one type of
exceptions, just not you write it catch(Foo) ... catch(Bar...). You very
rarely have functions that do exactly the same with two types, and only
those. The usage is different.

I do not think approach "it should look the same, no matter what the
usage is" is right.

> And vice-versa. The perception revolves around the fact that both appear
> to be signatured, regardless of how they're implemented in the engine.

But that's not what they do. Function says "I accept only parameter of
this type, and it's an error if it's not". Catch says "I will process
exception of this type, but if it's not, other catch clause may process
it". You don't chain functions like that, but you do chain catch
clauses. Again, different usage.
-- 
Stas Malyshev
smalyshev@gmail.com

---

## #92440 — Larry Garfield — 2016-04-19T03:01:17+00:00

URL: https://externals.io/message/92388#92440 · In reply to: #92437

> Hi!
>
> > […]
> You shouldn't be able to do this, because it makes no sense - why would
> a function accept two random types and only them? That's probably a bad
> design - it should be one type or two functions.

I detailed use cases for both & and | here:

http://news.php.net/php.internals/92335

Admittedly Foo|Bar is of limited use, although there is some use. 
Foo&Bar, however, has plenty of uses.

I do agree with the sentiment that having Foo|Bar for catch but not for 
function signatures is going to confuse people.  If nothing else, it 
will result in even more pressure to add union types.

--Larry Garfield

---

## #92441 — Stanislav Malyshev — 2016-04-19T05:10:41+00:00

URL: https://externals.io/message/92388#92441 · In reply to: #92440

Hi!

> Admittedly Foo|Bar is of limited use, although there is some use.
> Foo&Bar, however, has plenty of uses.

That is a different topic, not related to exceptions at all.

> I do agree with the sentiment that having Foo|Bar for catch but not for
> function signatures is going to confuse people. 

Confuse which people? Do people now are confused that you can write
multiple catch clauses for same exception but can't write multiple
function bodies for same parameter set? Are they confused by the fact
that it's a fatal error to pass wrong type to a function but catch just
silently ignores it? Are they confused that you can write
function(string $a) but you can't catch(string $a)? I don't think so. I
think people are pretty good at figuring out exceptions and function
parameters are different things. Java has multiple catch and no
expression types, and .net has the same after a fashion (with somewhat
weird syntax) but programmers on neither wonder around confused
expecting to find type calculus somewhere.
-- 
Stas Malyshev
smalyshev@gmail.com

---

## #92442 — Zeev Suraski — 2016-04-19T07:32:32+00:00

URL: https://externals.io/message/92388#92442 · In reply to: #92441

> -----Original Message-----
> From: Stanislav Malyshev [mailto:smalyshev@gmail.com]
> Sent: Tuesday, April 19, 2016 8:11 AM
> To: Larry Garfield <larry@garfieldtech.com>; internals@lists.php.net
> Subject: Re: [PHP-DEV] [VOTE] Catching Multiple Exception Types
> 
> 
> > […]
> 
> Confuse which people? Do people now are confused that you can write
> multiple catch clauses for same exception but can't write multiple function
> bodies for same parameter set? Are they confused by the fact that it's a fatal
> error to pass wrong type to a function but catch just silently ignores it? Are
> they confused that you can write function(string $a) but you can't catch(string
> $a)? I don't think so. I think people are pretty good at figuring out exceptions
> and function parameters are different things. Java has multiple catch and no
> expression types, and .net has the same after a fashion (with somewhat weird
> syntax) but programmers on neither wonder around confused expecting to
> find type calculus somewhere.


I agree.  But I think that if people are going to be confused and will pressure for consistency with functions, it's an exceptionally easy (again, no pun intended) to explain why they are in fact inherently different and why it makes sense here and doesn't make sense there.

Zeev

---

## #92446 — Zeev Suraski — 2016-04-19T09:56:09+00:00

URL: https://externals.io/message/92388#92446 · In reply to: #92414

> -----Original Message-----
> From: Derick Rethans [mailto:derick@php.net]
> Sent: Monday, April 18, 2016 6:18 PM
> To: Marco Pivetta <ocramius@gmail.com>
> Cc: Dan Ackroyd <danack@basereality.com>; Bronisław Białek
> <after89@gmail.com>; PHP internals <internals@lists.php.net>
> Subject: Re: [PHP-DEV] [VOTE] Catching Multiple Exception Types
> 
> 
> 
> > […]
> 
> I'm with you on this one.
> 
> > […]
> 
> Even this is a scary way to do it. As you don't know which exception class you
> have, how would you know how to handle each separately.

In reality, you often don't truly 'handle' exceptions at all.  Different exception types are very often just a slightly fancier and easier way to return errors, and it's actually very common and perfectly reasonable to handle groups of exceptions in an identical way.  It's also extremely common to see exception handlers that don't pay any attention to the exception object itself.  In fact, glancing through the Zend Framework 2 codebase and the Drupal 8 codebase, it seems that the exception object is rarely ever used. 

In Drupal 8, many catch blocks simply resort to return `FALSE` or TRUE.  For example:

    try {
      return $router->match($path);
    }
    catch (ResourceNotFoundException $e) {
      return FALSE;
    }
    catch (ParamNotConvertedException $e) {
      return FALSE;
    }
    catch (AccessDeniedHttpException $e) {
      return FALSE;
    }
    catch (MethodNotAllowedException $e) {
      return FALSE;
    }
  }

Note that this is in fact different from catch (Exception $e), and I'm guessing the author intentionally did not want to catch an exception the type of which she/he weren't expecting to get - and instead, have it propagate upwards.

With this RFC approved, it would become:

    try {
      return $router->match($path);
    }
    catch (ResourceNotFoundException|ParamNotConvertedException|AccessDeniedHttpException|MethodNotAllowedException $e) {
      return FALSE;
    }

Which is much, much nicer and readable.  And I found this in the ZF2 codebase:

                 } catch (DiRuntimeException $e) {
                     if ($methodRequirementType & self::RESOLVE_STRICT) {
                         //finally ( be aware to do at the end of flow)
                         array_pop($this->currentDependencies);
                         if (isset($alias)) {
                             array_pop($this->currentAliasDependenencies);
                         }
                         // if this item was marked strict,
                         // plus it cannot be resolve, and no value exist, bail out
                         throw new Exception\MissingPropertyException(
                             sprintf(
                                 'Missing %s for parameter ' . $name . ' for ' . $class . '::' . $method,
                                 (($value[0] === null) ? 'value' : 'instance/object')
                             ),
                             $e->getCode(),
                             $e
                         );
                     } else {
                         //finally ( be aware to do at the end of flow)
                         array_pop($this->currentDependencies);
                         if (isset($alias)) {
                             array_pop($this->currentAliasDependenencies);
                         }
                         return false;
                     }
                 } catch (ServiceManagerException $e) {
                     // Zend\ServiceManager\Exception\ServiceNotCreatedException
                     if ($methodRequirementType & self::RESOLVE_STRICT) {
                         //finally ( be aware to do at the end of flow)
                         array_pop($this->currentDependencies);
                         if (isset($alias)) {
                             array_pop($this->currentAliasDependenencies);
                         }
                         // if this item was marked strict,
                         // plus it cannot be resolve, and no value exist, bail out
                         throw new Exception\MissingPropertyException(
                             sprintf(
                                 'Missing %s for parameter ' . $name . ' for ' . $class . '::' . $method,
                                 (($value[0] === null) ? 'value' : 'instance/object')
                             ),
                             $e->getCode(),
                             $e
                         );
                    } else {
                        //finally ( be aware to do at the end of flow)
                        array_pop($this->currentDependencies);
                        if (isset($alias)) {
                            array_pop($this->currentAliasDependenencies);
                        }
                        return false;
                    }
                }

To save everyone some time, the two code blocks for the two exception types are completely identical.  I'm not sure why it wasn't function()alized, but regardless, it's another case where the new proposed feature would make perfect sense.

I think saying that identical handling for multiple exception types is bad or is grounds for an urgent code review is fairly disconnected from reality in many if not most cases.

Zeev

---

## #92447 — Ivan Enderlin — 2016-04-19T11:44:50+00:00

URL: https://externals.io/message/92388#92447 · In reply to: #92388

Hello,

Representing the Hoa project community, we have voted yes. Not everyone 
was agree (60% yes, 40% no), but my vote reflects the average.

Main argument in favor of no:
   * Encourage badly designed API.

Main argument in favor of yes:
   * This is not an edge case when dealing with badly designed API, so 
this is useful.

Cheers.


> Hi Internals,
>
> We've opened the vote on https://wiki.php.net/rfc/multiple-catch
> Voting will end on 2015-05-01

---

## #92450 — Dmitry Stogov — 2016-04-19T12:39:47+00:00

URL: https://externals.io/message/92388#92450 · In reply to: #92388

Only one note. I would prefer to use "," instead of "|" as a class name separator.
Especially if "Union types" are not accepted.
The rest should be fine.

Thanks. Dmitry.

---

## #92453 — Levi Morrison — 2016-04-19T13:09:26+00:00

URL: https://externals.io/message/92388#92453 · In reply to: #92450

> Only one note. I would prefer to use "," instead of "|" as a class name separator.
> Especially if "Union types" are not accepted.
> The rest should be fine.

On the other hand if union types is accepted it makes no sense to use "," here.

---

## #92454 — Zeev Suraski — 2016-04-19T13:10:25+00:00

URL: https://externals.io/message/92388#92454

> 
> 
> Only one note. I would prefer to use "," instead of "|" as a class name separator.
> Especially if "Union types" are not accepted.
> The rest should be fine.
> 

+1 from me as well on that.  We could have that change as a separate RFC at a later time, once things are clearer with union types.

Zeev

---

## #92456 — Bishop Bettini — 2016-04-19T13:24:54+00:00

URL: https://externals.io/message/92388#92456 · In reply to: #92437

On Mon, Apr 18, 2016 at 9:28 PM, Stanislav Malyshev <smalyshev@gmail.com>
wrote:

> Hi!
>
> > […]
>
> You shouldn't be able to do this, because it makes no sense - why would
> a function accept two random types and only them? That's probably a bad
> design - it should be one type or two functions.
>
> > […]
>
> But this makes total sense - you routinely catch more than one type of
> exceptions, just not you write it catch(Foo) ... catch(Bar...). You very
> rarely have functions that do exactly the same with two types, and only
> those. The usage is different.
>
> I do not think approach "it should look the same, no matter what the
> usage is" is right.
>
> > […]
>
> But that's not what they do. Function says "I accept only parameter of
> this type, and it's an error if it's not". Catch says "I will process
> exception of this type, but if it's not, other catch clause may process
> it". You don't chain functions like that, but you do chain catch
> clauses. Again, different usage.
>

// concrete example
// My application uses two implementation of collection pattern:
// one from eloquent and one from haldayne. I have a log method that
// logs value of either collection implementation and catches the same
// kind of invalid collection regardless of implementation
//
// in this example, both union types and multi-catch are available

namespace Eloquent;
class Collection implements Contract\Arrayable {
    public static function toArray() {
        if (! (is_array($this->data) || is_object($this->data))) {
            throw new Exception\UnexpectedValue;
        }
        // ....
     }
}

namespace Haldayne;
class Map implements ToArray {
    public static function toArray() {
        if (! (is_array($this->data) || is_object($this->data))) {
            throw new \UnexpectedValueException;
        }
        // ....
     }
}

namespace Application;
function log(Eloquent\Collection | Haldayne\Map $entity) {
    $logger->log($entity->toArray());
}

try {
    log(Config::find());
} catch (Eloquent\Exception\UnexpectedValue | \UnexpectedValueException
$ex) {
    die('Configuration is neither array nor object');
}

There is a pleasant symmetry in all of this. As a developer, I handle both
library's concrete collection implementations similarly for both happy and
unhappy paths, all using a compact expression (Class1 | Class2). The engine
helps me out here.

Now, imagine multi-catch passes (as I expect), but union types does not. I
would change my log function to this:

function log($entity) {
    if ($entity instanceof Eloquent\Collection | $entity instanceof
Haldayne\Map) {
        $logger->log($entity->toArray());
    } else {
        throw new \InvalidArgumentException;
    }
}

"Ugh, this is much easier in multi-catch. If only functions took multiple
types, this would be much simpler to write."

Now imagine union types pass, but multi-catch doesn't. Then I'd change my
catch:

try {
    log(Config::find());
} catch (Eloquent\Exception\UnexpectedValue $ex) {
    die('Configuration is neither array nor object');
} catch (\UnexpectedValueException $ex) {
    die('Configuration is neither array nor object');
}

"ugh, this is much easier in function types. If only I could catch multiple
exception types in one block."

So when I'm talking about "user confusion", I'm referring to a perception
of inconsistency by userland developers. And, I think we'd be well-wise to
avoid designing inconsistencies. So even though I consider both union types
and multi-catch to be on the rare side of utility, I consider having one
and not the other a worse situation than having neither.

---

## #92457 — Dmitry Stogov — 2016-04-19T13:25:16+00:00

URL: https://externals.io/message/92388#92457 · In reply to: #92453

> 
> > […]
> On the other hand if union types is accepted it makes no sense to use "," here.
Right. Lets take a final decision about syntax, after "Unoin Types" 
voting (but before 7.1 release).

---

## #92480 — Stanislav Malyshev — 2016-04-19T18:13:50+00:00

URL: https://externals.io/message/92388#92480 · In reply to: #92456

Hi!

> namespace Application;
> function log(Eloquent\Collection | Haldayne\Map $entity) {
>     $logger->log($entity->toArray());
> }

This is bad design. It assumes that two completely unrelated classes
have the same function and it means the same. If it's indeed true, we
have a way to express it. It's called "interface". If it's not true, and
you are just duck-typing, you should not pretend you use strict types,
and drop types completely. You shouldn't try yo sit on both chairs at
the same time and pretend you're using strict typing while in fact
duck-typing.

> "Ugh, this is much easier in multi-catch. If only functions took
> multiple types, this would be much simpler to write."

And it would be completely wrong, abuse of typing. If you mean to say
"anything that has log method", you should either have interface or just
get rid of the strictness and duck-type it and rely on PHP to throw
exception if anything happens. Otherwise it is just a sloppy design.

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #92485 — Bishop Bettini — 2016-04-19T19:18:50+00:00

URL: https://externals.io/message/92388#92485 · In reply to: #92480

On Tue, Apr 19, 2016 at 2:13 PM, Stanislav Malyshev <smalyshev@gmail.com>
wrote:

> Hi!
>
> > […]
>
> This is bad design. It assumes that two completely unrelated classes
> have the same function and it means the same.


Both of these exist in the wild, and both have identical function wrt
toArray: reducing a set of collection-like things to an array of arrays.


> If it's indeed true, we
> have a way to express it. It's called "interface".


Except these two are in separate libraries that cannot share an interface.


> > […]
>
> multiple types, this would be much simpler to write."
>
> And it would be completely wrong, abuse of typing. If you mean to say
> "anything that has [toArray] method", you should either have interface or
> just get rid of the strictness and duck-type it and rely on PHP to throw
> exception if anything happens. Otherwise it is just a sloppy design.
>

An interface is not possible: separate libraries. Duck-typing is not
appropriate: only these two specific classes are supported.

Union types solve certain problems better than equivalent run-time checks.
Multi-catch solves certain *other* problems better than equivalent
many-catch. They are orthogonal, but the syntax is similar: *"into this
block, accept this type or this type or ...."*

I contend that this apparent similarity must be consistent, lest we earn
another sadness.

---

## #92486 — Stanislav Malyshev — 2016-04-19T19:39:33+00:00

URL: https://externals.io/message/92388#92486 · In reply to: #92485

Hi!

> Except these two are in separate libraries that cannot share an interface.

If these are separate libraries, you can't be sure that method does the
same thing, or even that it will be existing in both.

> An interface is not possible: separate libraries. Duck-typing is not
> appropriate: only these two specific classes are supported.

This looks very strange and brittle design - two random classes from two
unrelated libraries, which you rely on have exactly the same
functionality and keep it that way. Not a robust design if you ask me.
Also, it looks strange why only those two random classes can be
converted to arrays but none others can. What if you had third class
that also can be converted to array? What if you had 20 of them? Again,
looks like brittle and ad-hoc design.

-- 
Stas Malyshev
smalyshev@gmail.com

---

Next page: https://externals.io/message/92388.md?page=2
