Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117445 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 85008 invoked from network); 29 Mar 2022 12:05:31 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Mar 2022 12:05:31 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 35BBA1804E3 for ; Tue, 29 Mar 2022 06:33:44 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.4 required=5.0 tests=BAYES_00,BODY_8BITS, NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8560 212.227.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.126.135]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 29 Mar 2022 06:33:43 -0700 (PDT) Received: from [192.168.179.4] ([85.212.104.104]) by mrelayeu.kundenserver.de (mreue011 [213.165.67.97]) with ESMTPSA (Nemesis) id 1M89XH-1ncmbm3dy7-005K7d for ; Tue, 29 Mar 2022 15:33:41 +0200 Message-ID: Date: Tue, 29 Mar 2022 15:33:41 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Content-Language: en-US To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K1:9Mb5rpB10GqajibBzq68MaQRoZiVU1QFxitwyW5LhJ2tOt1pFlN MIP2M3u95jaEySFcQDCiLG9A3TBeYR4nYdEdbtUnwjbEHjOHG8pX3uQPaUvmIw/YGY9u9Zs stbSYt6a6Ewe8+bVwC4ZVG7jU2Thb95ry4Lyi0UySbaoCd/vBMrMi3j4dtW4kTiV5v917kR zY0ZPJuOKXqFmpTIJj4Hw== X-UI-Out-Filterresults: notjunk:1;V03:K0:UIOD63dsryg=:KQXlplnznPtTM2BwJou793 8Z7zkl0fwbFStyMBZWt/LAMLICJEZvPxHycfuq6f0J0ATD/lOWLmO6zjavANavU9yrUdXswab vOap3EbbNC2lhSfeuQ81voPKMMfTGax8tI+mUPjC9a3A9n4OyK0AmZvIRF225yLSnsqxzpJZY dQlERwy629e9J2KoQ0QtgnaM395EBnPA5HxHhs0lmGkQi3R1/ALYj4A85jes9jM6LR7axATao 07v3n0upBHlfYXcIP4KDOOYsY/o9+tULnEEgpDi+EbG98PofdU+wCa77lny272MgOatqmXsNz vR4KymcCzDpZ67nX4nEtV5CBhcHK7b+gNCqUSc8fnD0fFBWDX/3VE9lJs09TTE8rTB8Y8HaX6 pBrriO4etq7Gv1u5pJXoWZpP3QpoXswhRfFxQLOs6xkuvYQ/6WgR9/LWHjI/u+58aoMwHKqJW NFZcjoYvoNmV0h1vawKxuNy6ZE7r5SDwEoitMtRrZFFZpkNNDY0Z5KRrFRcCDsCwnY949bgXP cBCmcHf4Ssxk085HOG/rqukfBItK4ynC7qq6fg0iU12tk7kOhvpkpzAZjDDjS10NOPr8JHM8J QniOXFBXu6vftLfoQPD3Yeu5Suil5Ib3sqiS32mXM5jc9D8Lz6uSNrPBjJBiV8Z0HQTBhIt/A rNanYrmRDVV1ZTzYKy8bfot74+mzVoVhXIaw8UvPMEELhnq+4TRHbcu6PrrF/fCVTju9DC4BR WnrRqJXn0UYCWjpO Subject: Re: [PHP-DEV] instance version of match ? From: thomas@nunninger.info (Thomas Nunninger) Hi, Am 29.03.22 um 14:34 schrieb Rowan Tommins: > On 29/03/2022 11:59, Robert Landers wrote: >>          $object instanceof AnotherInterface => 'bar', >> >> We can see that `SomeInterface` will resolve the interface and not the >> constant. > > > 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. > > >> 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. While I liked the intention of Karoly, I did not like the proposed magic. Would it be an idea (and possible) to extend the syntax somehow like: $result = match ($value) { instanceof MyObject => ..., >= 42 => ..., !== 5 => ... }; to be equivalent to: $result = match (true) { $value instanceof MyObject => ..., $value >= 42 => ..., $value !== 5 => ... }; Regards Thomas