I just realized that these are essentially equivalent in terms of what they match and how they'd be implemented:
[..start, 'a', 'b', ..middle, 'c', ..end] ->
"$(start)ab$(middle)c$(end)" ->
both would be nice for things like parsers and especially URL matching
e.g.
when url is
"/users/$(userId)/posts/$(postId)/edit" ->
Can this be implemented in an efficient way?
you'd need to implement this as basically a regex right?
naively it would be, for each pattern:
'a' above)a smarter version of that would avoid redoing work based on knowledge of the patterns in the other branches
e.g. if two patterns both start with "foo$(bar)baz and the first pattern matches all of that but then fails later, the second pattern should be able to pick up where the first one left off rather than having to re-match the whole thing from scratch
this would be nontrivial to implement in the compiler, but a sweet benefit of it is that userspace code would use it for the ergonomics and then get generated code that's way more efficient than what they'd likely get if they used any other ergonomic alternative
Yeah, I think we should support this. It is equivalent to an exceptionally limited version of regex, but I think that is a good thing. Just the most basic and sane pattern that is static.
Just have to decide some capture/association rules (which we could follow .* regex groups for).
Like how do we want it to match with duplication:
To be specific, we need to define the captures for this:
/users/joe/posts/posts/27/edit
I think the only reasonable option is to eagerly match on whatever is hardcoded in the pattern
so in that example, since /posts is written out in the pattern, the first /posts matches and then the second one goes into the variable (duplicates don't matter)
That is probably the best for performance.
this is the fastest algorithm
yeah exactly
Sounds good :+1:
yeah, the other thing I like about it is that it's basically not new syntax
just making existing syntax work in more places
we'd actually be removing a compiler error ("interpolation isn't allowed in patterns") and making it do something awesome instead :grinning_face_with_smiling_eyes:
Richard Feldman said:
I just realized that these are essentially equivalent in terms of what they match and how they'd be implemented:
[..start, 'a', 'b', ..middle, 'c', ..end] ->"$(start)ab$(middle)c$(end)" ->
I thought list patterns were only allowed to contain one ..rest pattern to avoid ambiguity on what it matches?
that's true today, but this is an idea for how to usefully remove that restriction :big_smile:
This looks neat! I think I'm answering my own question here, but how would type annotations work here? If I understand correctly, we wouldn't need type annotations. This would only work for strings, since what it de-sugars to would be a List of Strings, correct?
I think this pattern matching could work for both arbitrary lists with .. and also for strings with the interpolation syntax - it would be the same algorithm either way
just a question of whether it's looking at elements in a list or bytes in a string
Last updated: Jun 16 2026 at 16:19 UTC