This proposal was approved for Stage 1 in the May 2018 TC39 meeting, and
- slides for that presentation are available here.
- Its current form was presented to TC39 in the April 2021 meeting (slides).
Here are the links to the champions of the ECMAScript Pattern Matching proposal:
- Easily googleable; zero syntactic overlap
- Reduce reasons to reach for switch
- Preserve the good parts of switch
- Subsumption in this context means that the new pattern matching feature should encompass or replace the functionality provided by the existing
switchstatement in JavaScript. The idea is that pattern matching should be able to do everythingswitchcan do, and more, in a more expressive and safer way. In other words, pattern matching should makeswitchstatements obsolete or less necessary, because it can "subsume" the role ofswitch.
- The phrase "easily googleable" refers to the ability of developers to search for and find information about the feature online using search engines like Google. The goal is to design the syntax and terminology of pattern matching in a way that is distinctive and clear, so that developers can easily look it up and find relevant resources. If a feature has zero syntactic overlap with existing features, it reduces confusion and makes it easier to search for.
- Zero syntactic overlap means that the new pattern matching syntax should be distinct from the syntax of existing language constructs, like
switch. This ensures that there is no ambiguity or confusion when developers write code, and it also helps in making the feature "easily googleable."
- This phrase means that the introduction of pattern matching should minimize the need for developers to use
switchstatements. Pattern matching should provide a more powerful, expressive, and safer alternative, so that developers naturally prefer it overswitch.
- Finally, while the proposal aims to make pattern matching a superior alternative, it also acknowledges that
switchhas some useful features that should be retained. The goal is to incorporate the best aspects ofswitchinto the new pattern matching feature, ensuring that developers don’t lose any functionality they rely on. A good part of the switch is that it allows for an efficient translation to a jump table, which is a performance optimization that may be is not reachable when using pattern matching.
- No more footguns
- New capabilities
Footgun is a programming term used to describe features or behaviors in a language that can easily lead to errors, bugs, or unexpected behavior if used incorrectly. The term suggests that the feature is so error-prone that it is like "shooting yourself in the foot."
Some common "footguns" of the switch statement include:
-
Fall-Through Behavior:
- Fall-through is when execution continues from one
caseto the next, even if a match has been found, unless abreakstatement is used. This can lead to subtle bugs if the developer forgets to includebreakafter eachcase. - Example:
If the developer intended only "Case 1" to be logged when
switch (value) { case 1: console.log("Case 1"); case 2: console.log("Case 2"); // This will execute even if value is 1 }
valueis 1, this fall-through behavior can lead to unexpected results.
- Fall-through is when execution continues from one
-
Type Coercion:
- In
switchstatements, JavaScript performs strict comparison (===) for matching cases, but sometimes developers expect loose comparison (==). This can lead to bugs when values are implicitly coerced to different types. - Example:
Here,
switch ("2") { case 2: console.log("Number 2"); break; case "2": console.log("String 2"); break; }
"2"will not match2becauseswitchuses strict equality, which can be confusing for some developers.
- In
-
Non-Exhaustive Cases:
- Unlike pattern matching, where you can enforce handling all possible cases (often required in languages with exhaustive pattern matching),
switchin JavaScript does not require handling all possible cases. This can lead to missing cases and bugs. - Example:
If
switch (animal) { case "cat": console.log("Meow"); break; case "dog": console.log("Woof"); break; // Missing cases for other animals }
animalis not"cat"or"dog", nothing will happen, which might not be the intended behavior.
- Unlike pattern matching, where you can enforce handling all possible cases (often required in languages with exhaustive pattern matching),
- Pattern matching construct should be usable as an expression
return match { ... }let foo = match { ... }- etc
- Fall-through and "no match" should be opt-in, not opt-out
- Execution order should never be surprising
In the context of programming language design, particularly with respect to the "ECMAScript Pattern Matching" proposal, the terms "opt-in," "opt-out," and "fall-through" have specific meanings:
-
Opt-in: This means that a feature or behavior is not enabled by default and must be explicitly requested or activated by the developer. In other words, you have to "opt-in" to use it. For example, if a certain behavior (like fall-through in pattern matching) is opt-in, developers would need to explicitly write code or use a syntax to enable it.
-
Opt-out: Conversely, opt-out means that a feature or behavior is enabled by default, and the developer must take explicit action to disable or avoid it. If a behavior is opt-out, you are automatically opted in unless you specifically choose to turn it off.
-
Fall-through is a term typically associated with control flow structures, especially in the context of
switchstatements in languages like JavaScript. In aswitchstatement, fall-through occurs when the program continues executing the next case clause even after a match has been found, unless abreakstatement is used to prevent it.For example:
switch (value) { case 1: console.log("Case 1"); case 2: console.log("Case 2"); // Fall-through happens here case 3: console.log("Case 3"); }
In the above code, if
valueis1, all three cases will execute because there's nobreakaftercase 1.
When the proposal mentions that "fall-through and 'no match' should be opt-in, not opt-out," it is advocating for a design where these behaviors do not occur by default.
-
Fall-through being opt-in: The proposal suggests that in pattern matching, when a pattern matches, the execution should stop unless the developer explicitly indicates that they want to continue to the next pattern (i.e., fall-through). This would prevent unexpected behavior where multiple patterns could unintentionally be processed.
-
"No match" being opt-in: Similarly, if no pattern matches, the proposal suggests that the programmer should explicitly handle this case rather than having it automatically proceed to some default behavior. This ensures that "no match" situations are deliberately managed, reducing the risk of errors due to unhandled cases.
In summary, the statement advocates for a pattern matching design where behaviors like fall-through and handling of unmatched cases are only enabled when the developer explicitly chooses to do so, leading to safer and more predictable code.