tc39 /proposal-pattern-matching

Pattern matching syntax for ECMAScript

★ 5,803⑂ 90👁 303⑃ 84● 254▤ 0
HTMLMITCreated 2017-07-03tc39.es/proposal-pattern-matching/
+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.

 Exhau

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON