Re: [RFC] Deprecate implicitly nullable parameter type

From: Date: Wed, 24 Jan 2024 05:16:11 +0000
Subject: Re: [RFC] Deprecate implicitly nullable parameter type
References: 1 2  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
>
> I just encountered this language inconsistency when trying to remove
> nullable from a constructor-promoted property:
>
> ```
> class MyClass
> {
>     public function __construct(
>         public ?string $title = null // removing "?" here causes "Fatal
> error: Cannot use null as default value"
>     ) {
>        $this->title = $title ?: resolveTitle();
>     }
> }
> ```
>
> Which is fine, there's a simple workaround, it was just surprising.

Property types don't infer nullability from the default value being
null, so constructor properties don't allow it either. I'd say this is
not an inconsistency, but rather a *consistency* with what this RFC
proposes to deprecate and remove.

Highly supportive of this RFC :)


Thread (23 messages)

« previous php.internals (#122238) next »