Name: proposal-pattern-matching
Owner: Ecma TC39
Description: Pattern matching syntax for ECMAScript
Created: 2017-07-03 21:21:23.0
Updated: 2018-05-24 18:04:36.0
Pushed: 2018-05-15 19:01:40.0
Homepage: null
Size: 85
Language: null
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Stage: 1
Author: Kat Marchán (npm, @maybekatz)
Champions: Brian Terlson (Microsoft, @bterlson), Sebastian Markbĺge (Facebook, @sebmarkbage), Kat Marchán (npm, @maybekatz)
This proposal adds a pattern matching expression to the language, based on the existing Destructuring Binding Patterns.
There's many proposals potentially related to this one, and other proposals might mention interaction with this. This file includes casual, example-based discussion of the proposal, and there's also a document describing the core semantics in more formal language, which will be iterated over into the final Spec-ese.
There's also a document including suggestions for other future proposals, which are dependent on this one, but do not directly affect the main behavior of the feature.
This proposal was approved for Stage 1 in the May 2018 TC39 meeting, and slides for that presentation are available.
This proposal draws heavily from corresponding features in Rust, F#, Scala, and Elixir/Erlang.
Matching fetch()
responses:
t res = await fetch(jsonService)
(res) {
en {status: 200, headers: {'Content-Length': s}} -> {
console.log(`size is ${s}`)
en {status: 404} -> {
console.log('JSON not found')
en {status} if (status >= 400) -> {
throw new RequestError(res)
Terser, more functional handling of Redux reducers. Compare with this same example in the Redux documentation:
tion todoApp (state = initialState, action) {
se (action) {
when {type: 'set-visibility-filter', filter: visFilter} ->
return {...state, visFilter}
when {type: 'add-todo', text} ->
return {...state, todos: [...state.todos, {text}]}
when {type: 'toggle-todo', index} -> {
return {
...state,
todos: state.todos.map((todo, idx) => idx === index
? {...todo, done: !todo.done}
: todo
)
}
}
when {} -> {} // ignore unknown actions
Or mixed in with JSX code for quick props handling, assuming implicit do
:
ch url={API_URL}>{
ops => case (props) {
when {loading} -> <Loading />
when {error} -> <Error error={error} />
when {data} -> <Page data={data} />
when _ -> throw new Error('badmatch')
tch>
(via Divjot Singh)
General structural duck-typing on an API for vector-likes.
t getLength = vector => {
se (vector) {
when { x, y, z } ->
return Math.sqrt(x ** 2 + y ** 2 + z ** 2)
when { x, y } ->
return Math.sqrt(x ** 2 + y ** 2)
when [...etc] ->
return vector.length
ength({x: 1, y: 2, z: 3}) // 3.74165