On Tue, Jul 23, 2024, at 4:00 PM, Matthew Weier O'Phinney wrote:

>> However, a few people indicated a desire to have an explicit wildcard _ 
>> anyway, even if it's redundant, as it's a more common and standard approach 
>> in other languages.  We've indicated that we are open to making that an 
>> optional secondary vote in the pattern matching RFC if there's enough 
>> interest (it would be trivial), though I haven't bothered to add it to the 
>> RFC text yet.
>> 
>> Having _ available could also be used in other "wildcard" or "ignore this" 
>> cases, like exploding into a list assignment or similar, though I don't 
>> believe that has been fully explored.
>
> Can you provide examples of what that usage would look like? And the 
> question I have really is, does this actually _require_ using "_", or 
> could another token be used for such matches?

Hypothetical pattern matching example:

$foo is ['a' => int, 'b' => $b, 'c' => mixed];

That would assert that there's 3 keys.  "a" may be any integer (but only an 
integer), "b" can be anything and will be captured to a variable, and "c" must 
be defined but we don't care what it is.

The suggestion is to basically alias _ to "mixed" for pattern purposes:

$foo is ['a' => int, 'b' => $b, 'c' => _];

As "there's a var here but I don't care what it is, ignore it" is a common 
meaning of _ in other languages.  But that would need to be disambiguated from 
a pattern saying "c must be an instance of the class _".  Technically any 
symbol/set of symbols could be used there (as it's just an alias to mixed, 
which has the exact same effect), but _ is a common choice in other languages.

In theory, that could be expanded in the future to something like (note: this 
hasn't been seriously discussed that I know of, I'm just spitballing randomly):

[$a, $b, _] = explode(':', 'foo:bar:baz');

To assign $a = "foo", $b  to "bar", and just ignore "baz".  Which might cause 
parser issues if _ is a legal class name, I'm not sure.  There's probably other 
"ignore this" cases we could come up with, but I haven't actually thought about 
it.

Again, whether any of the above is a compelling argument or not I leave as an 
exercise for the reader.

--Larry Garfield

Reply via email to