Re: [RFC] Deprecate implicitly nullable parameter type
On 22 January 2024 10:21:12 GMT, tag Knife <[email protected]> wrote:
>As you are mistaking iint $var = null
params as "nullable". Which they
>are not, they are "optional default" parameters.
The feature which is being discussed is that, for the specific case of "= null", the
parameter is made both optional *and* nullable.
To make it clearer, the following all declare default values within the allowed type:
int $foo = 1
int|string $foo = 'hello'
?int $foo = 1
?int $foo = null
The following all lead to a type error, because the default value isn't allowed for the
declared type:
int $foo = 'hello'
int|string $foo = new DateTime;
?int $foo = 'hello'
However, there is a special case: for purely historical reasons, a default of null is allowed *even
when it doesn't match the declared type*:
int $foo = null
int|string $foo = null
These are processed as though they were declared as nullable types; and the fix for the proposed
deprecation would be to do so:
?int $foo = null
int|string|null $foo = null
The fact that the feature is tricky to explain is a good reason to deprecate it, and I think I
support the proposal unless I see a good argument against.
Regards,
--
Rowan Tommins
[IMSoP]
Thread (23 messages)