Re: [RFC] [PRE-VOTE] Union types

From: Date: Sun, 05 Jun 2016 08:15:18 +0000
Subject: Re: [RFC] [PRE-VOTE] Union types
References: 1 2 3 4 5 6 7 8 9 10 11  Groups: php.internals 
Request: Send a blank email to internals+get-93801@lists.php.net to get a copy of this message
maybe you can add a few examples to the type table in the rfc, so everybody knows how it actually works: function test(int | float | string $a) {var_dump($a);} test(42.0); // float(42) test('42'); // string(2) "42" function test2(int $a) {var_dump($a);} test2(42.0); // int(42) test2('42'); // int(42) function test3(int|bool $a) {var_dump($a);} test3('foo'); // true Regards Thomas Bob Weinand wrote on 03.06.2016 14:30: > >> Am 3.6.2016 um 14:18 schrieb Rowan Collins <rowan.collins@gmail.com>: >> >> On 03/06/2016 12:59, Bob Weinand wrote: >>>> function l(string | float $x) { echo gettype($x); } >>>> l("1.5a"); // I would expect string, but float would succeed if attempted >>> >>> Exact matches *ALWAYS* match without coercion. >> >> Gah, I keep making that mistake in my examples. I did say I was struggling to >> get my head around things! ;) >> >> I'm still missing an explanation of exactly where you derived the current >> proposed rules from, though. The RFC just states "these rules are not an >> invention of this proposal", but goes on to say "applies PHP casting rules in >> a sane way", which implies some decisions were involved in drawing up these >> rules. >> >> I think you may be right that the rules are as simple as they can be, and I >> don't want to waste too much of your time, but a basic summary of the >> reasoning used would be much appreciated. >> >> Regards, >> -- >> Rowan Collins >> [IMSoP] > > The *weak casting* rules (i.e. what gets converted how and what can be > converted to the other type) are not an invention of the proposal. > > The proposal however defines the specific order. > > I’ve added a reasoning why not left-to-right now. As this seemed to be asked > multiple times. > > Also, the specific reasoning why I propose that specific order the rules are > in: >> Otherwise PHP's casting rules are applied in an order to be as lossless as >> possible. > > That’s really the only motivation behind that. > > Bob > > > > >

« previous php.internals (#93801) next »