Re: [Early Feedback] Pattern matching

From: Date: Tue, 25 Jun 2024 07:22:51 +0000
Subject: Re: [Early Feedback] Pattern matching
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-123805@lists.php.net to get a copy of this message
On 25 June 2024 00:31:56 BST, Larry Garfield <larry@garfieldtech.com> wrote: > >* There's not been much discussion of range patterns. Anyone want to weigh in on those? I didn't even notice them until someone else mentioned some detail of the syntax. Like regex patterns and the generic-like array syntax, they look really useful, but also like something that should be standardised across the language, and not just snuck in to patterns. I don't mean we have to implement "foreach(1..10 as $b)" at the same time as "$a is 1..10", necessarily, but we're going to hate ourselves if we rush in a range syntax for patterns then realise we *can't* reuse it elsewhere for some reason. My personal preference would be to keep the first RFC as focused as possible, so that we have time to discuss everything in it. Additional patterns could be an immediate follow-up RFC within the same release cycle. So I think that means the first RFC covering: - The concept of patterns - The "is" keyword - match integration - Variable binding - Variables in expressions (as you say, we need to at least discuss the syntax for these at the same time as variable binding) - Type patterns - Literal patterns - Class constant patterns - Compound patterns (unions and intersections) - Array destructuring-like patterns - Object property patterns (these feel essential, but notably are the only thing on this list not based on an existing syntax) . That's still a big list of things to discuss. Once we have the concept of patterns in the language, there's plenty of scope for adding more of them, and more places to use them, but let's not be too hasty in throwing every shiny feature in at once. >* No one has really weighed in on nested patterns for captured variables. Any thoughts there? They feel almost-essential to me, because it feels odd to have to choose to *either* specify an element *or* capture it, as in "$foo is [int, string]" vs "$foo is [$a, $b]". On the other hand, in their full glory they allow for some extremely complex patterns, and that might put some people off. As with variables in patterns, we probably need to at least consider *how* to write them before committing to the overall syntax. e.g. my earlier suggestion of "$foo=" for binding fits very naturally with "$foo=int"; but if we had ">>$foo" it would feel more natural to write "int>>$foo", and would that order be better or worse? Regards, Rowan Tommins [IMSoP]

« previous php.internals (#123805) next »