On Wed, Mar 20, 2024 at 1:47 PM Robert Landers <landers.rob...@gmail.com> wrote: > > On Tue, Mar 19, 2024 at 10:06 PM Rowan Tommins [IMSoP] > <imsop....@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.