Re: Re: [RFC] [PRE-VOTE] Union types
| From: | Rowan Collins | Date: | Fri, 03 Jun 2016 10:28:04 +0000 |
| Subject: | Re: Re: [RFC] [PRE-VOTE] Union types | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-93741@lists.php.net to get a copy of this message | ||
On 03/06/2016 11:16, Thomas Bley wrote:
why not try all types weakly from left to right in weak mode? Rule: if type check would give a fatal error for a type, try next type.This is what I suggested, but Bob is insistent that weak mode should always select the same type as strict mode where there is a valid strict match, which I guess makes sense. A strict left-to-right check violates that: function i(string | int $x) { echo gettype($x); } i(10); // string in weak mode, int in strict mode function j(int | string $x) { echo gettype($x); } j('10'); // int in weak mode, string in strict mode The main complication that I haven't quite got my head round yet is the fact that an int->float coercion is allowed in strict mode. So to be consistent, weak mode needs to privilege that cast: function k(string | float $x) { echo gettype($x); } k(10); // float But in other cases it doesn't seem sensible to privilege float over string: function l(string | float $x) { echo gettype($x); } l("1.5a"); // I would expect string, but float would succeed if attempted Regards, -- Rowan Collins [IMSoP]