Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:93725 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 7194 invoked from network); 2 Jun 2016 17:43:17 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Jun 2016 17:43:17 -0000 Authentication-Results: pb1.pair.com header.from=bobwei9@hotmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=bobwei9@hotmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain hotmail.com designates 65.55.111.80 as permitted sender) X-PHP-List-Original-Sender: bobwei9@hotmail.com X-Host-Fingerprint: 65.55.111.80 blu004-omc2s5.hotmail.com Received: from [65.55.111.80] ([65.55.111.80:60661] helo=BLU004-OMC2S5.hotmail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 88/27-62101-33070575 for ; Thu, 02 Jun 2016 13:43:17 -0400 Received: from BLU436-SMTP18 ([65.55.111.71]) by BLU004-OMC2S5.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.23008); Thu, 2 Jun 2016 10:43:13 -0700 X-TMN: [BVX2Kex/FuUh74NcRxVh6QjeK77G0+3i] X-Originating-Email: [bobwei9@hotmail.com] Message-ID: Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 (Mac OS X Mail 9.2 \(3112\)) In-Reply-To: Date: Thu, 2 Jun 2016 19:43:07 +0200 CC: internals@lists.php.net Content-Transfer-Encoding: quoted-printable References: <0A.C5.62101.1C860575@pb1.pair.com> To: Rowan Collins X-Mailer: Apple Mail (2.3112) X-OriginalArrivalTime: 02 Jun 2016 17:43:11.0089 (UTC) FILETIME=[3B27B610:01D1BCF6] Subject: Re: [PHP-DEV] Re: [RFC] [PRE-VOTE] Union types From: bobwei9@hotmail.com (Bob Weinand) > Am 02.06.2016 um 19:33 schrieb Rowan Collins = : >=20 > On 02/06/2016 18:11, Andrea Faulds wrote: >> That aside, the behaviour the RFC now specifies for how weak typing >> interacts with union types is frighteningly complicated. I don't see = how >> it could be anything other than that, but the new complexity this >> introduces to PHP is enough for me to vote against this RFC, even >> ignoring my other concerns. >=20 > That is rather a long-winded set of rules, I agree. I wonder if it's = possible for the order the union type is defined to be taken into = account? That way you could specify the rule thus: >=20 > * The type is tested against each type in the union in turn. > * If weak typing is active, scalar types will be coerced where this = would normally be allowed, and passed over where a TypeError would be = thrown. > * If strict typing is active, scalar types will be treated the same as = non-scalar types, and must match precisely. >=20 >=20 > So: >=20 > function f(int | float $number) { echo gettype($number); } > f('10'); // int >=20 > function g(float | int $number) { echo gettype($number); } > g('10'); // float >=20 > function h(int | Foo | string) { echo gettype($number); } > h('10'); // int > h(new Foo); // object > h('hello'); // string >=20 > function i(string | int) { echo gettype($number); } > i('10'); // string > i(10); // string >=20 > This last example is the only one that might be a bit surprising - the = "| int" in the type def is actually doing nothing - but there's always = scope for user error. >=20 >=20 > In strict mode, things would be a bit different, because no coercion = is allowed: >=20 > declare(strict_types=3D1); >=20 > function f(int | float $number) { echo gettype($number); } > f('10'); // [!] TypeError >=20 > function g(float | int $number) { echo gettype($number); } > g('10'); // [!] TypeError >=20 > function h(int | Foo | string) { echo gettype($number); } > h('10'); // string > h(new Foo); // object > h('hello'); // string >=20 > function i(string | int) { echo gettype($number); } > i('10'); // string > i(10); // int >=20 >=20 > It may be that the implementation makes this prohibitively difficult, = but it would certainly be easier to document than the rules currently = proposed. >=20 > Regards, > --=20 > Rowan Collins > [IMSoP] We had that exact idea relatively early, but it exposes other = problems=E2=80=A6 order suddenly matters. You cannot just add "a" type = and get the expected results. E.g. function f(true | string $foo) { ... } everything except 0, =C2=B10, "0" and "" would now return true. That's = totally WTF. Sure, it's more friendly for people who want to read = *rules*. But it is very bad for intuitivity. Also: > function i(string | int) { echo gettype($number); } > i('10'); // string > i(10); // string This. This is especially bad as it has different behaviors in strict and = weak mode then. That's a no-go. The current way is the only viable one, as far as we discovered. Bob=