Re: [RFC] Deprecate implicitly nullable parameter type

From: Date: Mon, 22 Jan 2024 10:45:36 +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
On Mon, Jan 22, 2024 at 11:21 AM tag Knife <[email protected]> wrote:

> On Mon, 22 Jan 2024 at 09:51, Gina P. Banyard <[email protected]> wrote:
>
> > Hello internals,
> >
> > Máté Kocsis and myself would like to propose deprecating implicitly
> > nullable parameter types.
> >
> > The RFC is available on the wiki at the following address:
> > https://wiki.php.net/rfc/deprecate-implicitly-nullable-types
> >
> >
> > Best regards,
> >
> > Gina P. Banyard
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit: https://www.php.net/unsub.php
> >
> >
> Also just did a quick test, and i don't believe this would be possible. It
> would remove functionality from the language widely used.
> As you are mistaking iint $var = null params as "nullable". Which they
> are not, they are "optional default" parameters. The equals
> can be succeeded by any value, to set the parameter to such variable where
> the parameter is not passed. int ?$arg is not provided as
> an alternative, the directive int ?$arg simple allows the parameters to
> be passed as null, it is still required to pass through the
> parameter.
>
> To clarify
>
> T ?$var = Requires the parameter to be passed  to the function, null
> value allowed
> T $var = null = Does not require the parameter to be passed to the
> function, is set to null if not.
>
> https://3v4l.org/IWsqK


Your example contains int2 as type with a default value of null. This
works because it implicitly marks the class int2 as nullable. If you were
to set this default value to 1 via int2 $int2 = 1 it breaks with
"Fatal
error: Cannot use int as default value for parameter $int2 of type int2"

Anyhow, = null implicitly makes the parameter nullable even if the type
doesn't specify it's nullable. The argument is that if the type isn't
nullable, then the default value may not be null either.


Thread (23 messages)

« previous php.internals (#122219) next »