Re: Proposal: AS assertions
| From: | Marco Aurélio Deleu | Date: | Tue, 19 Mar 2024 18:04:02 +0000 |
| Subject: | Re: Proposal: AS assertions | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-122693@lists.php.net to get a copy of this message | ||
Marco Deleu
> On 19 Mar 2024, at 14:51, Ilija Tovilo <tovilo.ilija@gmail.com> wrote:
>
> 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
That looks like a PHP dream. Has there been any work regarding that?