Re: [RFC] Nullable Casting
| From: | Claude Pache | Date: | Mon, 08 Apr 2019 15:41:22 +0000 |
| Subject: | Re: [RFC] Nullable Casting | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-105150@lists.php.net to get a copy of this message | ||
> Le 8 avr. 2019 à 12:24, Stephen Reay <php-lists@koalephant.com> a écrit :
>
> if your target ’type’ is
?int and you pass an empty string,
> wouldn’t you expect to get null rather than
> 0 ?
You should get the same value than you get in:
```php
declare(strict_types=0);
function foo(?int $x) {
return $x;
}
foo("");
```
BTW, this is an argument to the have nullable casting functionality in core, rather than leave each
developer to craft their own solution. Because PHP does already such nullable castings in several
occasions (typed parameters, typed return values, and soon typed class properties, when you are in
strict_types=0 mode), we should offer the programmers a built-in evident way to do the same type of
casting, with the same semantics in occasion they want to do it manually. Otherwise, one developer
will decide to map the empty string to null, the other to 0 and the third to NAN, and they get
problems when they refactor the code in a way that a manual casting is replaced by an automatic
implicit casting or vice versa, and the semantics differs on some cases.
—Claude