Skip to content

Pattern Matching (Stage 0) #6

@hzoo

Description

@hzoo

Original issue submitted by @xtuc in babel/babylon#610

This issue is to keep track of the implementation in the parser of this proposal.

Champions: Brian Terlson (Microsoft, @bterlson), Sebastian Markbåge (Facebook, @sebmarkbage)
Spec Repo: https://github.com/tc39/proposal-pattern-matching

tc39/proposals#56

Remember that proposals are just that; subject to change until Stage 4!

Examples

let length = vector => match (vector) {
    { x, y, z }: Math.sqrt(x ** 2 + y ** 2 + z ** 2),
    { x, y }:   Math.sqrt(x ** 2 + y ** 2),
    [...]:      vector.length,
    else: {
        throw new Error("Unknown vector type");
    }
}
let isVerbose = config => match (config) {
    { output: { verbose: true } }: true,
    else: false
}

let outOfBoundsPoint = p => match (p) {
    { x > 100, y }: true,
    { x, y > 100 }: true,
    else: false
}

You can find more example in the spec repo.

Parsing

https://github.com/babel/babylon/blob/master/CONTRIBUTING.md#creating-a-new-plugin-spec-new

  • Babylon plugin: patternMatching

Transform

  • Babel plugin

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions