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