Re: instance version of match ?
| From: | Rowan Tommins | Date: | Tue, 29 Mar 2022 12:34:01 +0000 |
| Subject: | Re: instance version of match ? | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-117443@lists.php.net to get a copy of this message | ||
On 29/03/2022 11:59, Robert Landers wrote:
Yeah, the instanceof operator is magic in that regard - it has a special parsing rule to consume the next token and avoid it being evaluated as a constant.$object instanceof AnotherInterface => 'bar',We can see thatSomeInterfacewill resolve the interface and not the constant.
I think what they are proposing is that when the match is an object,
and the branches are class/interface/etc names, it should just do an
instanceof operation instead of a value-equals operation.
That wouldn't work, because the type of value passed to match() can vary at run-time, but you'd need to compile the expression one way or the other.
If it did work, it would be extremely confusing:
function example(string|object $input) {
return match($input) {
SomeClass => 'found class',
SOME_CONSTANT => 'found constant',
};
}
var_dump( example(new SomeClass) );
var_dump( example(SOME_CONSTANT) );
Do both of those matches succeed? What if I set `const SomeClass = 'hello';`?
So unfortunately we need some extra syntax to say that something should be an instanceof check, and therefore a class name.
Regards,
--
Rowan Tommins
[IMSoP]