Re: Proposal: AS assertions

From: Date: Wed, 20 Mar 2024 12:51:15 +0000
Subject: Re: Proposal: AS assertions
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-122699@lists.php.net to get a copy of this message
On Wed, Mar 20, 2024 at 1:47 PM Robert Landers <landers.robert@gmail.com> wrote: > > On Tue, Mar 19, 2024 at 10:06 PM Rowan Tommins [IMSoP] > <imsop.php@rwec.co.uk> wrote: > > > > On 19/03/2024 16:24, Robert Landers wrote: > > > > $x = $attributeReflection->newInstance() as ?MyAttribute; > > if ($x === null) // do something since the attribute isn't MyAttribute > > > > > > I think reusing nullability for this would be a mistake - ideally, the right-hand side > > should allow any type, so "$foo as ?Foo" should mean the same as "$foo as > > Foo|null". > > > > > > A better alternative might be to specify a default when the type didn't match: > > > > $x = $attributeReflection->newInstance() as ?MyAttribute else null; > > if ($x === null) // do something since the attribute isn't MyAttribute > > At that point, you are just reinventing already existing things. If > you wanted to do something like that with my proposal: > > $x = $attributeReflection->newInstance() as ?MyAttribute ?? > MyAttribute::createDefault(); > > Robert Landers > Software Engineer > Utrecht NL Oh and there isn't any difference between: $x as ?Type or $x as Type|null The codebase I work with prefers ? over |null, but they are the same.

« previous php.internals (#122699) next »