Re: Proposal: AS assertions
| From: | Ilija Tovilo | Date: | Tue, 19 Mar 2024 16:50:55 +0000 |
| Subject: | Re: Proposal: AS assertions | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-122690@lists.php.net to get a copy of this message | ||
Hi Robert
On Tue, Mar 19, 2024 at 5:24 PM Robert Landers <landers.robert@gmail.com> wrote:
>
> I've been thinking about this as an RFC for awhile, but with generics
> being far off (if at all), I'd like to propose a useful idea: reusing
> the AS keyword in a different context.
>
> Example:
>
> $x = $attributeReflection->newInstance() as MyAttribute;
>
> This would essentially perform the following code:
>
> assert(($x = $attributeReflection->newInstance()) instanceof MyAttribute);
See https://wiki.php.net/rfc/pattern-matching#throwing_alternative.
I
believe this idea would combine nicely with pattern matching. It has
many more uses there than just simple class type matching, and could
even be used for things like destructuring.
Ilija