Re: [RFC] Nullable Casting
| From: | Benjamin Morel | Date: | Wed, 10 Apr 2019 16:15:46 +0000 |
| Subject: | Re: [RFC] Nullable Casting | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-105213@lists.php.net to get a copy of this message | ||
>
> I believe returning null in those situations makes the most intuative
> sense, yes.
No offense, but how can casting semantics different from those already used
in implicit casting be intuitive?
function a(int $x) { var_export($x); }
function b(?int $x) { var_export($x); }
a("123"); // 123
a(null); // TypeError
a("abc"); // TypeError
b("123"); // 123
b(null); // NULL
b("abc"); // TypeError
IMO, the only reasonable choice is therefore:
(int) "123"; // 123
(int) null; // TypeError
(int) "abc"; // TypeError
(?int) "123"; // 123
(?int) null; // NULL
(?int) "abc"; // TypeError
At least, if we want to fix the current behaviour, and not introduce
another inconsistency.
In other words, *strictly apply to explicit casting the rules of implicit
casting when strict_types is disabled*.
--
Ben