RFC: a reserved keyword case
for further exploring pattern match in Julia
#50875
Replies: 1 comment
-
I would like the parser to do this. I think and Where those line numbers are the beginning of the pattern and result. We would expect to use it in pattern-matching macros like this: @switch <intput> begin
case pattern:
result
case pattern if condition:
result
end or this @switch <intput> begin
case pattern: result
case pattern if condition: result
end The advantage of including the result as part of the syntax of the case is that the |
Beta Was this translation helpful? Give feedback.
-
From JuliaCon 2023, we discussed if we can support a better pattern match in Julia with @NHDaly @c42f and I just chatted with @gafter. And I believe multiple people have realized that it is very hard to support good pattern expression within the current syntax because they tend to have lots of ambiguities semantically. One example is the implementation of guards, currently this is usually done with the following expression in (MLStyle, Match, Rematch, etc.)
where the
where
is usually ambiguous becausewhere
may annotate type params, and the first and thrid expression has the extra boilerplates that one needs to type every time, plus it cannot limit one to not writeif cond stmts end
in the pattern because it's a valid syntax.So here my proposal is to have a reserved keyword
case
with the following semantics:case <pattern> : <expression>
orcase <pattern> if <cond>: <expression>
which returns the following syntax object
and
and perhaps the corresponding line number nodes. I think this solves the ambiguity problem and will allow people further exploring pattern match in Julia within packages. I'd like to see what people think? and if there are potential issues with this? Or maybe there are better syntax that resolves the ambiguity when served as a pattern expression? I think in general there aren't much options for arrows, which is why I think an extra (reserved) keyword is needed.
Beta Was this translation helpful? Give feedback.
All reactions