Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123732 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 C04131A009C for ; Fri, 21 Jun 2024 15:36:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1718984245; bh=B1PmvtwplLpXGqczuA8MYAERINFOz7BQGXDannq+ndg=; h=References:In-Reply-To:From:Date:Subject:To:From; b=PTWUDicckoyYh6CMkY3jBtqTWSYJy61WgDg49vc1J1PvW5XY98u+fnYWJKT7LRnE6 VUX3v6cjr5nNI5ukSkuQMNjVY/Jv8snTUL43HhcKoEPgfhHQbBbgePoRGpUKiNYd0G 2nAn1maLxWXY007AR6M5QMPijfF6JMSan4l4Z3HZb6e2Daw9jo3AppWFSGwZBxxzjU q6zsWQzYIPq08Z1r1ypnp59iS7wtUizTW+ONL0iTnkeucvlWE4hbxg8e3B+tptvjBh 7J4hSEaeTyYwomt9cuLI3M4Md3TLc3E1QKHV/chjSzV7FiE0ZyZ3lfVFWu1uMNV24E 4JVuXmavSlA2Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DFBB518005C for ; Fri, 21 Jun 2024 15:37:22 +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.8 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_ENVFROM_END_DIGIT,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=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 mail-vk1-f169.google.com (mail-vk1-f169.google.com [209.85.221.169]) (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 ; Fri, 21 Jun 2024 15:37:19 +0000 (UTC) Received: by mail-vk1-f169.google.com with SMTP id 71dfb90a1353d-4ef54d10bb6so151180e0c.1 for ; Fri, 21 Jun 2024 08:36:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718984164; x=1719588964; darn=lists.php.net; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=3cWLvfCMCbw7TBnRh8UPTOeIcsEojKuiQLFFEr08GQo=; b=LhOHFjc0/4nudK+ZVIzRl5AcL7qiqXkD2gWbOhF3Ylq7nWdgV79XeQeOwOXOSbK+0I l260miGzTkLtzshQIxiKCy10KUcFnSZLPZ0LiWWL9BRc4EXOu9Em9taHAihHIWbPAlM6 v4LPqHTr7ge2zf1tURaeqvc1xghViCo6AhyLEG/1AMCOaaCnOlXuIYRCs0p6Ezoufyw/ LgT4Gv/9HeTIsBPzrjKwHrrMXKS6+OPYAXktaf11b2LNf4ChU+/keTha3TKG2wOvSvql 5+PvLspbRzO4Go95ie4rLKjGWQM85/Bjfb2knT5hHgNZ5V3/PBI11KadRMiBMy6uiE/C cMSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718984164; x=1719588964; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3cWLvfCMCbw7TBnRh8UPTOeIcsEojKuiQLFFEr08GQo=; b=Cd4H5Iyq5VHIETzK4p3BCTsua7KaHwIaZqlBsk3TSv4tYs44SGBD2enTBPXNi/sGcd nQnWQT2jgTSu3xSnvFi25ezgaHDsYn96bniun1q4nLAcijVTdbqxlfwTMXXUC6k32p25 gPQPbx/B986iVQLy6KbI9LG7N2o/4P0cr7MiMaaw3yFJhHu6XtqKA3vhjxGY5GId9N1S GtMvwZilJw1azBjiTdO78jL04Bgx0dmwFMVGkV4qMXJZNAnrMUDj/Y6qlieYp3gr/Ej6 1qXr0Y/Zw0v4QTr/9AHHwiZw+JT4e2eqKlfz4fHVD4/4VAZ8//PCKcVyd/AvOXuIk/oW 74fQ== X-Gm-Message-State: AOJu0YxSrQFdHK525ePaauk8yaWBd4w1aQkP2Qc1POiwcrENYBwRi0PA 4kYlgdCWgzlRgh/uV0uuYguMwnvCuVltTpTZw1XcDwPWw4T3iytns+KpwQT91GP8ZjUaLwfMM7y 13bO29RdZ7Hknl+1tK6Ge/Zl9GFMeXiBb X-Google-Smtp-Source: AGHT+IEW4x06VE8bHbb6x5w/0Z9/YZPGp6pM4RXnjnopJLHPMKLWY256lP12ns9I9w9MOx08i+Ivu0rqSVUSaItJsUA= X-Received: by 2002:a05:6122:264b:b0:4ef:27e0:3f8c with SMTP id 71dfb90a1353d-4ef27e0419dmr5439885e0c.0.1718984164100; Fri, 21 Jun 2024 08:36:04 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <2a6b92eb-d5e9-4a1a-9548-a068ac42ebd2@app.fastmail.com> In-Reply-To: <2a6b92eb-d5e9-4a1a-9548-a068ac42ebd2@app.fastmail.com> Date: Fri, 21 Jun 2024 10:35:53 -0500 Message-ID: Subject: Re: [PHP-DEV] [Early Feedback] Pattern matching To: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: brandonja991@gmail.com (Brandon Jackson) > Ilija and I have been working on and off on an RFC for pattern matching s= ince the early work on Enumerations. A number of people have noticed and s= aid they're looking forward to it. Hi Larry, I have definitely been looking forward to this. Perhaps more so than property hooks and avis. > By "very high level," I mean, please, do not sweat specific syntax detail= s right now. That's a distraction. What we're asking right now is "which = of these patterns should we spend time sweating specific syntax details on = in the coming weeks/months?" There will be ample time for detail bikeshedd= ing later, and we've identified a couple of areas where we know for certain= further syntax development will be needed because we both hate the current= syntax. :-) I think that a lot of this would be best broken up. Although much of it is aimed towards the same general idea, a lot of the pieces have specific use cases and special syntax additions. Overall I think this rfc should be simplified to just pattern matching with the `is` keyword with the patterns limited to what can be declared as property types (DNF types) and future scoping everything else. Maybe possibly with the addition of 1 or 2 of the top requested `is` pattern matching capabilities as secondary votes. 1. `as` `is` and `as` have different responsibilities. I'm guessing the idea is to keep them in sync. But I would still like to see this as a future scope with a separate rfc. I do like the idea, and believe it's much needed. But I think the pattern matching portion `is` overshadows the `as` portion causing it not to get as much attention as far as discussion and analysis goes. Especially if the idea is to sync them, then that makes `as` just as big of an addition as `is` For example, what if instead, a generally prefered solution/syntax were variable types, something like: `var Foo&Bar $my_var =3D $alpha->bravo;` Although casting and declaring are 2 separate things. This seems like it would accomplish the same thing without the extra keyword, with the exception of casting being inline. How much would it get used if both were added? Would one then become an "anti-pattern"? 2. Literal patterns Again a really nice addition. Love it and likely will be loved by many. Although, it definitely deserves its own separate discussion. Looking at typescript which has both enums and literal types (although vastly different in php) caused what was once considered a nice feature to be black listed by many. Also note how typescript separates type land and value land. Maybe worth considering. 3. Wild card Has already been marked as not necessary it looks like and replaced by mixe= d. 4. Object matching Absolutely a separate rfc please. Definitely needs discussion. Could intersect another potentially preferred solution like type aliases. Sending one or the other into anti-pattern world. Maybe a solution similar to this would be preferred: ```php type MyType =3D Foo&Bar $foo =3D $bar as MyType|string ``` Or maybe not, either way I think it needs its own spotlight. 5. Array sequence patterns More in depth discussion needed. Not sure how often this comes up as a structure people want to check against, but it can definitely be done in user land with array slices. Even though it might be nice for completion's sake, it may not be worth it if there's not high demand for it. If at all could be grouped with associative array patterns. 6. Associative array patterns Love to have this one, but it also seems like a small extension or conflicting with array shapes. Also potentially conflicts with generics (which may or may not ever be a thing) but still let's give it the attention it needs. Maybe group with array shapes as well. 7. Array shapes Same as above 8. Capturing values out of a pattern and binding them to variables if match= ed Ok I think that's stepping a bit far out of scope. Maybe `is` should simply check and not have any side effects. 9. match .. is Nice shorthand to have but i'd rather not see short hands forced in as an all or nothing type thing as was done with property hooks. I'd also argue that maybe short hands should not be added until a feature has been around for at least one release and is generally accepted. That way we're not using up syntaxes and limiting the ability to add other syntax features without breaking backwards compatibility. Keep in mind that the `is` functionality alone allows this (or at least it should) and a shorter version may or may not be desired. ```php match(true) { $var is Foo =3D> 'foo', ... } ``` ** This is not an order of preference by any means, just listed as seen in the rfc. ** I'll stop there, and hope the message is received. In summary I would be plenty grateful for just being able to check against DNF types initially with support for more pattern types in the near or distant future. I think array shapes and literal types are the only ones I'd hope for a sooner rather than later follow up rfc targeted hopefully for the same release. Maybe even as secondary votes. The others I'm ok with waiting for and would rather see follow up rfcs on them as time permits.