Re: [RFC] Nullable Types
| From: | Thomas Bley | Date: | Thu, 21 Apr 2016 21:47:20 +0000 |
| Subject: | Re: [RFC] Nullable Types | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-92611@lists.php.net to get a copy of this message | ||
> Note that the same /would/ be possible with union types if given precedence:
>
> function fn(): int|false {}
false is currently not supported in the union_types rfc.
Regards
Thomas
Fleshgrinder wrote on 21.04.2016 21:33:
> On 4/21/2016 6:33 PM, Thomas Bley wrote:
>> Hello Tom,
>>
>> with default return value I mean to return a certain value if nothing else is
>> returned (similar to method parameters with a default value).
>>
>> example 1:
>>
>> declare(strict_types=0);
>>
>> function my_strpos(string $haystack, string $needle): int = false {
>> return 42; // return 42
>> return false; // return false
>> return true; // return 1
>> return; // return false
>> }
>>
>> example 2:
>>
>> declare(strict_types=1);
>>
>> function my_strpos(string $haystack, string $needle): int = false {
>> return 42; // return 42
>> return false; // return false
>> return true; // fatal error
>> return; // return false
>> }
>>
>
> I definitely do not like the equal sign in there because it goes against
> all my mathematical knowledge; an int is false?!?
>
> Note that the same /would/ be possible with union types if given precedence:
>
> function fn(): int|false {}
>
> That being said, it is way too magic! A return without an argument is
> *void* and has to result in a
TypeError. Really, start to think
> about
> the types as /checked annotations/ ...
>
> /**
> * @return int|false
> */
> function fn() {}
>
> function fn(): int|false {}
>
> ... are equivalent and the former does not suddenly return *false* on
> its own and hell it shouldn't.
>
> --
> Richard "Fleshgrinder" Fussenegger
>
>