Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123711 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id B88BB1A009C for ; Thu, 20 Jun 2024 20:33:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1718915664; bh=esGc7Nhq5mcgqCeomeWyWmdGWs9vCpzyEQCepYo1+RE=; h=In-Reply-To:References:Date:From:To:Subject:From; b=Qh/vZ6E3e2ym8dYUaWpXMdqGdnaveV3emEByMbPQVK7jvDdivUvho/duHEhVfLdtt +iyfBO/i+hSTXlu/kYCzrxx5Bv02B6JfII0X2khhOEr3pdQkDTDkULX3TyxkzBr4we RbzdJq2EruUf6Pw/rKg9nV7CfQhmWvWkQ559zAuv2pkkogR/1DtNjt+grlRRikpKNT tmk+zldtUXdFQRRJoXDnwM+f+6DbcqFRqYmhkL4Wk4HjLeESRWjivyvRc+fDJ9NL8h iyFIL+KIQGAgIwuGWCrhvzgz5SH47w30qDANWgeC/t1vdIaGxXhnXBhfKBzXqbCIIn Xz0zQ2cNJjUUw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5368C180A55 for ; Thu, 20 Jun 2024 20:34:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from wfout1-smtp.messagingengine.com (wfout1-smtp.messagingengine.com [64.147.123.144]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 20 Jun 2024 20:34:18 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfout.west.internal (Postfix) with ESMTP id 0CB131C0012B for ; Thu, 20 Jun 2024 16:33:03 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Thu, 20 Jun 2024 16:33:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to; s=fm2; t=1718915583; x=1719001983; bh=TPtG669WKLlQw1vD8gVua qc2/xkrJwqWnvSbKkqC5p0=; b=sXUuwlcOzYLyx9j6dU0+D/mlxw3SIOX+UURiG UqizU0smZ/TcmaKkmvJnVofuvxX7s6xFFWtDLXbzfjjmjsVdlMZwMzi00Y54++5J 9L/LAUpRqoTVd0E5v10cA2mFQKuIw3Z4JoOteYCJCQuvLMkRlsc7pIBDYDzzq9HY T3eDxG7mKI/qK+YgMWeLx+Lbd1HkY5CjM011CBjb3ddtmMkll4DhufevN0uwYOY7 dJHXpCruBhmSnEmFpdFrUiiS+DFZdZvd6IqPeK++JcZiOfu9YeLr9dJQEe9/KVmz Rn9kBUk7muZjEDqIfCGgb4iB5R3eQ4P2Nzd7NtySVTDiJHFBA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1718915583; x= 1719001983; bh=TPtG669WKLlQw1vD8gVuaqc2/xkrJwqWnvSbKkqC5p0=; b=k Tnbbsb75eAaRh0Put9U6ff30WQNLMXjJsGiqKl5ZREqLvIeVessn0cg6/koW7Gvo eQo2wN/U95Mr4rZHjr7IfdS6tIp74FbHJ9ppQEhBfjRdtljUz9iNHdvrpo+nXfuA rBbX0RtzNmLqS9Ad6zoU3e7KYP3SpL/X0E8BOX76TYjcLMjGMYFZzBWoyKYGIRBU 3Qbf8+fwm/nluqAyEwe9T5e4mLNgYEEaXZQb3vpiADAfmkOy6xr1e9Ic08/LOmlZ dZ+OMHo28F17kO/JEjgp1mdsshsLjZKq1foieEeOIrR3Z4xx3YVHcbRpBJe8ItpM Qfz2sqxSY/5q6/nGx8Bdg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrfeefvddgudehvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvufgtgfesthhqredtreerjeenucfhrhhomhepfdfn rghrrhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrd gtohhmqeenucggtffrrghtthgvrhhnpeeggeehgfetjeehgefggefhleeugefgtdejieev vdethfevgeeuudefleehvdetieenucffohhmrghinhepphhhphdrnhgvthenucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrghr fhhivghlughtvggthhdrtghomh X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 765E41700093; Thu, 20 Jun 2024 16:33:03 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-522-ga39cca1d5-fm-20240610.002-ga39cca1d Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: <43ee6c4d-a254-44ca-ae4f-e4d794ee0c81@app.fastmail.com> In-Reply-To: References: <2a6b92eb-d5e9-4a1a-9548-a068ac42ebd2@app.fastmail.com> Date: Thu, 20 Jun 2024 20:32:43 +0000 To: "php internals" Subject: Re: [PHP-DEV] [Early Feedback] Pattern matching Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable From: larry@garfieldtech.com ("Larry Garfield") On Thu, Jun 20, 2024, at 8:22 PM, Lynn wrote: > On Thu, Jun 20, 2024 at 7:41=E2=80=AFPM Larry Garfield wrote: >> https://wiki.php.net/rfc/pattern-matching > I have been looking forward to this RFC, it's such a quality of life t= o=20 > be able to do all this! In terms of things to focus on, I'd personally=20 > be very happy with the property/param guards, "as" and "is ",=20 > but I won't say no to anything we can get extra here because it's all=20 > really nice to have. > > I noticed that with array structure patterns the count is omitted when=20 > using ... > ```php > if ($list is [1, 3, ...]) { > print "Yes"; > } > // True. Equivalent to: > if (is_array($list)=20 > && array_key_exists(0, $list) && $list[0] =3D=3D=3D 1=20 > && array_key_exists(1, $list) && $list[1] =3D=3D=3D 3 > ) { > print "Yes"; > } > ``` > Wouldn't this need a `count($list) >=3D 2`? I'm not sure if the=20 > underlying mechanism does the count check as well, but it seems to me=20 > like a guard clause for performance reasons in the PHP variant.=20 At the moment, the implementation doesn't actually compile down into tho= se primitives; it has its own all-C implementation. Having it instead c= ompile down to those operations is something Ilija is exploring to see h= ow feasible it would be. (The main advantage being that the optimizer, = JIT, etc. wouldn't have to do anything new to support optimizing pattern= s.) The examples shown in the RFC for now are just for logical equivale= ncy to explain the functionality. In this case, the array_key_exists() = checks are sufficient for what is actually being specified, so the count= () is redundant. The final implementation will almost certainly be more= performant than my example equivalencies. :-) > Maybe a tangent, what about iterators? Not supported, as you cannot examine them "all at once", by definition. = I don't even know what an iterator-targeted pattern would look like, th= ough if someone figured that out in the future there's no intrinsic reas= on such a pattern couldn't be added at that time. > "Limited expression pattern" > I think this would be very valuable to have, though in the proposal it=20 > seems cumbersome to use regardless of syntax. It feels like I'm going=20 > to be using the variable binding less often than matching against othe= r=20 > variables, what about an "out" equivalent? > > ```php > $result =3D match ($p) is { > Point{x: 3, y: 9, $z} =3D> "x is 3, y is 9, z is $z", > Point{$x, $y, $z} =3D> "x is $x, y is $y, z is $z", > }; > // vs > $x =3D 3; > $y =3D 9; > $result =3D match ($p) is { > Point{x: 3, y: 9, out $z} =3D> "x is 3, y is 9, z is $z", > Point{$x, $y, out $z} =3D> "x is 3, y is 9, z is $z", > }; > ``` > To me this makes it much more readable, just not sure if this is even=20 > feasible. This is not meant as bikeshedding the syntax, more of an=20 > alternative approach to when to use which. A couple of people have noted that. Assuming at least one of those two = synaxes makes it into the initial RFC (I think variable binding has to f= or it to be really useful), we'll have a whole separate sub-discussion o= n that, I'm sure. Though, I would expect variable binding to be used more than expressions= , not less, which would make the marker make more sense on the expressio= n. But that's something to bikeshed later. --Larry Garfield