Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117471 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 77506 invoked from network); 31 Mar 2022 17:26:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 Mar 2022 17:26:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D0C1C1804D0 for ; Thu, 31 Mar 2022 11:55:32 -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.7 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (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 ; Thu, 31 Mar 2022 11:55:32 -0700 (PDT) Received: by mail-wm1-f54.google.com with SMTP id p12-20020a05600c430c00b0038cbdf52227so194242wme.2 for ; Thu, 31 Mar 2022 11:55:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2FSOnADWvVUQWd5tXIVXSJ+dWhMVuEfYGYvjX/BXqQY=; b=LmqTBhNMVHhDloEfCdzD4Ga9dGsQSuajHlsW7Lxtvnf/toBA6oZoJizeJlBr/Pokhi 6qiWyg7VbyysI47dTAVDq7sdZnFsI/kfgwuowwFKVXh5Amlm82PuvZcAUFh/K2UE+PR5 kg897h5y5h9yzaDqZNQubOlfEuYyrldUSmTgBbaWv+rPhtSeM0JWhEvmtWEPggxmjWlZ HxYLc9lm4USgUvardf4eHV9theUZHy/9AaNMgPMdqB7ZQVW/8QVf2afd70/gW+Pco5B5 afFY6xosU7x1u475y9d7HDetKNeAQMtcJQjvI2JFB9opY4Eg4uHINvzhnNSpizxCXNuR YcRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2FSOnADWvVUQWd5tXIVXSJ+dWhMVuEfYGYvjX/BXqQY=; b=XKEZiYhXtRfZ/PuSd3Z0bqmC8U7LzSO/vGid2GT+GQ6cl2FIHkzKdrp80dDy5MPWJG fRtfHbr5IEMxPQVjF/1nrOwJa82jtR5onYm0oGthiBdw1z3Od0DhITM2J/Oke+2WXiVS nhDE0QuvIlufNn5dv9gX9iR+e6sgg7MrcWluWtUcj++e5dg0x67UnbYr35+abz+3JIJj GWnG9h1itT0WChaHRS26zxzcXj/K+fbuJ+Ek9rzMgaIgc0r2s6ujyDTC0zQR5zL4VrKE 7S8cSRUU/8XQL7No5AZ9+lcjP9yO0pzl+ByC/HJdV75EUlkYYs7/irtVb5Z3mqCfm1Jm M48Q== X-Gm-Message-State: AOAM5338GnPaE8XjuBs/4Owtb4rIBAgJzS/hpzSCk0Z5Vz97WP1xmmR0 CAmmhvI3XdLgKaYEiRN1woqQ4ggkvqWMIw== X-Google-Smtp-Source: ABdhPJxOgrGLzQE9oe+XTPAOuNlJplHiBg69JQiyPsfzuMOMJj9pOxd63YY35G7IEnryBpX49FdeBQ== X-Received: by 2002:a05:600c:2052:b0:38c:be16:aeae with SMTP id p18-20020a05600c205200b0038cbe16aeaemr5574001wmg.157.1648752930560; Thu, 31 Mar 2022 11:55:30 -0700 (PDT) Received: from arnaud-t490.localnet (2a01cb04054b5b00e3cbc3ad95fab935.ipv6.abo.wanadoo.fr. [2a01:cb04:54b:5b00:e3cb:c3ad:95fa:b935]) by smtp.gmail.com with ESMTPSA id g10-20020adfe40a000000b00203eb3551f0sm224040wrm.117.2022.03.31.11.55.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Mar 2022 11:55:29 -0700 (PDT) To: internals@lists.php.net Cc: Thomas Nunninger Date: Thu, 31 Mar 2022 20:55:28 +0200 Message-ID: <3318808.5fSG56mABF@arnaud-t490> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Subject: Re: [PHP-DEV] instance version of match ? From: arnaud.lb@gmail.com (Arnaud Le Blanc) On mardi 29 mars 2022 15:33:41 CEST Thomas Nunninger wrote: > 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 I like this. The pattern matching RFC may also cover this: https://wiki.php.net/rfc/pattern-matching -- Arnaud