Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92490 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 28020 invoked from network); 19 Apr 2016 20:19:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Apr 2016 20:19:13 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 77.244.243.87 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 77.244.243.87 mx106.easyname.com Received: from [77.244.243.87] ([77.244.243.87:58668] helo=mx201.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FF/17-22821-0C296175 for ; Tue, 19 Apr 2016 16:19:12 -0400 Received: from cable-81-173-133-226.netcologne.de ([81.173.133.226] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1asc73-0007cr-Ds; Tue, 19 Apr 2016 20:19:09 +0000 Reply-To: internals@lists.php.net References: <571581C7.7010901@gmail.com> <571589AF.3090502@gmail.com> <5716755E.1040807@gmail.com> <57168975.8090202@gmail.com> To: Stanislav Malyshev , bishop@php.net Cc: PHP internals Message-ID: <571692B1.9050501@fleshgrinder.com> Date: Tue, 19 Apr 2016 22:18:57 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 MIME-Version: 1.0 In-Reply-To: <57168975.8090202@gmail.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="Ob0hlgBs06JItm5HwRt194gUlVCRwLKN8" X-ACL-Warn: X-DNSBL-BARRACUDACENTRAL Subject: Re: [PHP-DEV] [VOTE] Catching Multiple Exception Types From: php@fleshgrinder.com (Fleshgrinder) --Ob0hlgBs06JItm5HwRt194gUlVCRwLKN8 Content-Type: multipart/mixed; boundary="0UicKIClI9cBE5po69klxd5RRuEhshnPS" From: Fleshgrinder Reply-To: internals@lists.php.net To: Stanislav Malyshev , bishop@php.net Cc: PHP internals Message-ID: <571692B1.9050501@fleshgrinder.com> Subject: Re: [PHP-DEV] [VOTE] Catching Multiple Exception Types References: <571581C7.7010901@gmail.com> <571589AF.3090502@gmail.com> <5716755E.1040807@gmail.com> <57168975.8090202@gmail.com> In-Reply-To: <57168975.8090202@gmail.com> --0UicKIClI9cBE5po69klxd5RRuEhshnPS Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 4/19/2016 9:39 PM, Stanislav Malyshev wrote: > Hi! >=20 >> Except these two are in separate libraries that cannot share an interf= ace. >=20 > 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. >=20 >> An interface is not possible: separate libraries. Duck-typing is not >> appropriate: only these two specific classes are supported. >=20 > This looks very strange and brittle design - two random classes from tw= o > 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. >=20 Stanislav is completely right here, this is exactly one of the usages of union types that is completely wrong. You had this example: 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'); } The problem with this is that you now require both libraries to be installed at the same time because otherwise the union type cannot be verified by PHP's type system. This will bite you in the ass in no time. Just think of your composer: "require": { "eloquent/collection": "*", "haldayne/map": "*" } Both do the same?!? Better rely on duck typing in such cases. function log($entity) { if (method_exists($entity, 'toArray')) { $logger->log($entity->toArray()); } else { // handle is in another way. } } If you really want to ensure that the method is not being called by a dev in that way go for assertions. function log($entity) { assert('method_exists($entity, "toArray")', 'entity must implement `toArray` method'); // Check in production too to avoid fatal errors? if (method_exists($entity, 'toArray')) { $logger->log($entity->toArray()); } } Union types only have a few special use cases but those are worth exploring imho. function div(int|float $x, int|float $y): int|float { if (is_float($x) || is_float($y)) { return $x / $y; } else { return intdiv($x, $y); } } function even(int|string $x): bool { if (is_int($x)) { return $x % 2 =3D=3D=3D 0; } else { return gmp_mod($x, 2) =3D=3D 0; } } --=20 Richard "Fleshgrinder" Fussenegger --0UicKIClI9cBE5po69klxd5RRuEhshnPS-- --Ob0hlgBs06JItm5HwRt194gUlVCRwLKN8 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJXFpK2AAoJEOKkKcqFPVVrwN8P/jYpnjGSb0k2WjsU/BSW7kgh OQhe1DaLmw+HkVt+aaK1sRxwdUkwlU0yIyjtbhFqQQQE4iwhlRzzXKF2DxR5b9hl koxY3owQmP2roV0oQO9g6/7XpuSs5CibI8lzFqgsk5ftd+QJUI9aGucN2aoGj2FD 3R9iUJYD1SsabLgyVPdBm+MudofnQs7CJarAUddM1hBYa1UAcdhOEGhQQueCNChz zgueb8/Rxu/TWHWSVvlxPcK3YQCrrJ6K7xz5qwPKHbcE9RmvY4giqm0Zuc+Rwt3p 5h8gW/o4R9+HDrCFo5T/WlW+PuRd5OyPdlNZYiQNNIjn76tDccRkE3nwE68iJf+H TYwg0ORIuzdjcaBF2MGaQxhRRJtP0y8WS9qcreqP30lO+i0Hr1kHubJ9lFzEABh5 x6d46Hdm4GMlRTAleK3EoAPb4hRMPFpPV0BVNqd1/A2gjvURUfIS6q7ACY16iCzP TBf6+Kma1aOTimDtE5kecXS2ytq40zdZ5/ywpt75UfNwleRh3gOGzjea60psupyO kQzbrS7ATZ5v/KKT8Htztg/yg6YqHW7SsoO7mw+3SfkAOn1NW/eAq5rFb27hwdDN BjaFYjJJCTWTSZP1XBgHgL/4e/vL2/cYVP7XicAHptHfpud3cE9EoLiKC3lYmuJi mVxBz1smt5q6JMT1hF2D =t3zQ -----END PGP SIGNATURE----- --Ob0hlgBs06JItm5HwRt194gUlVCRwLKN8--