Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:93732 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25250 invoked from network); 2 Jun 2016 20:25:09 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Jun 2016 20:25:09 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.46 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.46 mail-wm0-f46.google.com Received: from [74.125.82.46] ([74.125.82.46:36586] helo=mail-wm0-f46.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 33/00-24756-42690575 for ; Thu, 02 Jun 2016 16:25:09 -0400 Received: by mail-wm0-f46.google.com with SMTP id n184so96638344wmn.1 for ; Thu, 02 Jun 2016 13:25:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:references:from:to:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding; bh=IFPjNb+1zpn/47ZTKALIp1zHuGKdcsYyocnzka8SvjU=; b=mFelaHHlAnQc5Wyc30CVxur+qmEbr0P/SoifxRX0/607+BbLYNYo82b0ldl6DIdqGU a/T69v3JpoVb0Pu1uAOOJlt0RiNB+EcFNq3UlGtY05yiS7n2WVRyg5ddC1X1pRONTr90 fD1mBXZV8AH+826YveQ4oPh1p0KvPXsnoV++9gVgQVklaPWwoVtV8yNd98XgYVeswX6I L0cDjp0GWIo7v+VpMZ9w8+g6HukbPUnkjdxJizu+mQ3vLmDwKhS0WNJyz6SPJp5xtzw4 C7lL1EL3ZL49situviaMKS9lpD5GPseAV1U2ZJJblHGEoCIkL+ZDUO5/Tr5hcEwh7KAV ud4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:references:from:to:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=IFPjNb+1zpn/47ZTKALIp1zHuGKdcsYyocnzka8SvjU=; b=LYu4ypVCGyGjklPySGYZs0/2eRFb0E/uFdYMcgQkHLIl0+hzH19UlU+SuSBS5/7iIu LmxtGZWDii6UQSNm5wemlBsvH125BCTF6FCDRHsYreIluY6Rsps9yImydccp3SX0ERBM tyfmBTHplUQ2fXDr4tkNJv4o96g211Vur12CIMjWJ7fFLi08CiaIuX01SrolsO4SvyX2 FvwV5dqFk/tMfDqQmzPzIU9RAiIFhhF6HL2w/2ZHg6cXHIAvtLmvvl8WBvVm7TI2niIk PZhh174Ouu6exQdecgDR1EFA4/8rRt4e50FvRL7bXzTxAKQKz1ZWeZv/R/Akz+MOa8Wr LmbQ== X-Gm-Message-State: ALyK8tL6BZW7aX26lVtuQokPEr27Z0Cymjlr1E3mjTtjGuqxQxY1w0AK/QZxGkioOR/WbQ== X-Received: by 10.28.230.200 with SMTP id e69mr506433wmi.53.1464899105090; Thu, 02 Jun 2016 13:25:05 -0700 (PDT) Received: from [192.168.1.5] ([2.25.96.65]) by smtp.googlemail.com with ESMTPSA id c7sm2200665wji.38.2016.06.02.13.25.04 for (version=TLSv1/SSLv3 cipher=OTHER); Thu, 02 Jun 2016 13:25:04 -0700 (PDT) References: <0A.C5.62101.1C860575@pb1.pair.com> To: internals@lists.php.net Message-ID: <68b821ac-d71f-4be5-8dca-ae94db332630@gmail.com> Date: Thu, 2 Jun 2016 21:25:01 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.1.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Re: [RFC] [PRE-VOTE] Union types From: rowan.collins@gmail.com (Rowan Collins) On 02/06/2016 18:43, Bob Weinand wrote: > We had that exact idea relatively early, but it exposes other problems… order suddenly matters. You cannot just add "a" type and get the expected results. > > E.g. > function f(true | string $foo) { ... } > > everything except 0, ±0, "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. I'm not so sure about that - the RFC already mentions the mnemonic that "|" means "or", and anyone reading PHP code should be familiar with short-cutting boolean operators, so this feels kind of natural to me: "$foo must be true OR a string". Thus "I would prefer it to be true, but if not, will accept a string". > 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. Again, the logic is "I would prefer a string if you can, but an int if not"; if weak mode tries its best to match that specification, it will always land on a string coercion. It's more of a problem the other way around, though, because I'd forgotten that weak mode is allowed to perform lossy casts: function f ( int | string $number ) { echo $number; } f('1a'); In weak mode, this would echo "1", because the int cast succeeds, lossily. That's a little odd, I admit. The rules are much clearer in table form, by the way, thumbs up for that. :) Although it would be nice to point to some documentation of where these rules were lifted from, given the claim that "they are not the invention of this proposal". Looking at them, I see there is one extra rule which doesn't seem to be consistent with normal weak typing, but is trying very hard to be lossless, and that's "float -> int (if lossless)" as a separate priority from "float -> int". Current loose typing performs no such check: function f(int $x) { echo gettype($x), ':', $x; } f(4.5); // integer:4 But the proposal is that this will prefer a string a cast: function f(int | string $x) { echo gettype($x), ':', $x; } f(4.5); // string:4.5 If you get rid of this extra rule, the order of checks actually becomes a simple priority order, regardless of source type: [exact match], int, float, string, boolean. If the aim is to be lossless, then perhaps the order could be tweaked to make lossy casts always lower priority: string, float, int, boolean? That's technically lossy when passing int(9007199254740993) to "float | int" because it's above the safe threshold of 2^53, but the current implementation is already lossy when passing it to "float | string": function f(float | string $x) { var_export($x); } f(9007199254740993); // 9007199254740992.0 Regards, -- Rowan Collins [IMSoP]