Re: [RFC] [PRE-VOTE] Union types
| From: | Bob Weinand | Date: | Fri, 03 Jun 2016 11:59:31 +0000 |
| Subject: | Re: [RFC] [PRE-VOTE] Union types | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-93742@lists.php.net to get a copy of this message | ||
> Am 3.6.2016 um 12:28 schrieb Rowan Collins <rowan.collins@gmail.com>:
>
> 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
Exact matches *ALWAYS* match without coercion.
Emphasis from the RFC:
Primarily, this issue is *avoided if a parameter type exactly matches the input type* or if PHP is
in strict type mode.
> Regards,
> --
> Rowan Collins
> [IMSoP]
Bob