+0
日增长
5,803
stars
90
forks
303
watchers
84
prs
254
issues
0
releases
README
ECMAScript Pattern Matching
Status
Stage: 1
Spec Text:
Authors: Originally Kat Marchán (Microsoft,
@zkat\_\_); now, the below champions.
Champions: (in alphabetical order)
- Daniel Rosenwasser (Microsoft,
@drosenwasser)
- Jack Works (Sujitech, @Jack-Works)
- Jordan Harband (HeroDevs, @ljharb)
- Mark Cohen (@mpcsh\_)
- Ross Kirsling (Sony, @rkirsling)
- Tab Atkins-Bittner (Google, @tabatkins)
Introduction
Problem
There are many ways to match values in the language,
but there are no ways to match patterns
beyond regular expressions for strings.
However, wanting to take different actions
based on patterns in a given value
is a very common desire:
do X if the value has a foo property,
do Y if it contains three or more items,
etc.
Current Approaches
switch has the desired structure --
a value is given,
and several possible match criteria are offered,
each with a different associated action.
But it's severely lacking in practice:
it may not appear in expression position;
an explicit break is required in each case to avoid accidental fallthrough;
scoping is ambiguous
(block-scoped variables inside one case are available in the scope of the others,
unless curly braces are used);
the only comparison it can do is ===; etc.
if/else has the necessary power
(you can perform any comparison you like),
but it's overly verbose even for common cases,
requiring the author to explicitly list paths into the value's structure multiple times,
once per test performed.
It's also statement-only
(unless the author opts for the harder-to-understand ternary syntax)
and requires the value under test to be stored in a (possibly temporary) variable.
Priorities for a solution
This section details this proposal’s priorities. Note that not every champion
may agree with each priority.
_Pattern_ matching
The pattern matching construct is a full conditional logic construct that can do
more than just pattern matching. As such, there have been (and there will be
more) trade-offs that need to be made. In those cases, we should prioritize the
ergonomics of structural pattern matching over other capabilities of this
construct.
Subsumption of switch
This feature must be easily searchable, so that tutorials and documentation are
easy to locate, and so that the feature is easy to learn and recognize. As such,
there must be no syntactic overlap with the switch statement.
This proposal seeks to preserve the good parts of switch, and eliminate any
reasons to reach for it.
Be better than switch
switch contains a plethora of footguns such as accidental case fallthrough and
ambiguous scoping. This proposal should eliminate those footguns, while also
introducing new capabilities that switch currently can not provide.
Expression semantics
The pattern matching construct should be usable as an expression:
- return match { ... }
- let foo = match { ... }
- () = match { ... }
- etc.
The value of the whole expression is the value of whatever clause is
matched.
ExhauTop issues
#322Proposed simplification & increased symmetry53 comments · 2024-04-09#324Do we need iterator/object property caching?35 comments · 2024-04-11#304Dropping `when` keyword from match clauses?17 comments · 2023-10-03#337Clause separators11 comments · 2024-12-10#175Path to Stage 4!11 comments · 2021-04-21#325Disallow { a }9 comments · 2024-04-12#305Could we re-evaluate what our "must-haves" are?9 comments · 2023-10-05#294Statement version of match expression8 comments · 2023-09-04