Re: [RFC] Nullable Casting
| From: | Mark Randall | Date: | Wed, 10 Apr 2019 23:45:15 +0000 |
| Subject: | Re: [RFC] Nullable Casting | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-105229@lists.php.net to get a copy of this message | ||
On 10/04/2019 22:30, Benjamin Morel wrote:
Your arguments are perfectly valid and make sense, I guess this is a matter of not viewing different semantics for explicit and implicit cast as inconsistencies.It's an interesting discussion and I appreciate you engaging on it. I wonder, perhaps, if the goals of your initial RFC could be achieved by a single operator? If your intent is to pass through null and convert everything else, then it seems to me what you're really after is a reverse null coalescing operator, a binary operator that takes the right hand side only if the left hand side is not null, although I'm struggling to think of any character combination to represent it. Although you have to write the variable twice, it seems more generic... Admittedly I'm unsure which real-world cases it would be relevant to. $nullable_number = $maybe_null ^?? (int)$maybe_null; Personally I would have maybe approached it a different way and changed casting itself to contain an ordered list, where the first convertible match is returned. $nullable_number = (null|int)$input; $input is null, null is a legit cast to null, so matches first condition and returns null. Otherwise moves on to next option and casts it to an integer. -- Mark Randall