Re: Proposal: AS assertions
| From: | Robert Landers | Date: | Wed, 20 Mar 2024 12:47:59 +0000 |
| Subject: | Re: Proposal: AS assertions | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-122698@lists.php.net to get a copy of this message | ||
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