instance version of match ?
| From: | Karoly Negyesi | Date: | Mon, 28 Mar 2022 23:55:26 +0000 |
| Subject: | instance version of match ? | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-117440@lists.php.net to get a copy of this message | ||
Hi,
May I suggest adding a little "instanceof" magic to match? Like this:
match ($object) {
Someinterface => 'foo',
AnotherInterface => 'bar',
}
this can not clash with any existing code as using identifiers like this
are a syntax error currently.
Currently you could do the same with
match (true) {
$object instanceof Someinterface::class => 'foo',
$object instanceof AnotherInterface::class => 'bar',
}
Indeed, the former could be considered syntactic sugar for the latter. And
I do not think there's any ambiguity here for the casual reader or is it
just me who thinks no other sensible reading is possible?
Thanks for your consideration.
Karoly Negyesi