Description
Pattern matching for Nim
Pattern matching can be a good addition to the stdlib/fusion. It seems it becomes more mainstream:
- Rust : https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html
- Python : https://www.python.org/dev/peps/pep-0622/
- Ruby : https://rubyreferences.github.io/rubyref/language/pattern-matching.html
This is an early version of a RFC: we need to decide what to do before having a more complete document.
We already have several pattern matching libraries :
- patty by Andrea Ferretti https://github.com/andreaferretti/patty
- ast-pattern-matching by Arne Doring https://github.com/krux02/ast-pattern-matching
- gara by myself https://github.com/alehander92/gara/ (not well maintained)
- possibly others? maybe case statement macro dsl-s? please write a comment if you want to add something
A first question is, what do we want:
- just keep it in third party libraries
- standartize one and put it in fusion
- invent a new dsl based on case stamenets/better research of other languages and put it in fusion
- standartize one and put it in stdlib
- invent a new dsl based on case stamenets/better research of other languages and put it in stdlib
Motivation
- Easier to write complicated conditions
- Easier to extract values
- More flexible syntax
TODO
case a:
of Circle(r: @b):
echo b
of c@Other:
echo c
else:
discard
Many more examples in gara, ast_pattern_matching and patty's docs: TODO when a general approach is decided on
Constraints
- compile time: ast and other values : as @krux02's ast-pattern-matching
- runtime for most nim types
- extensible
- zero/low overhead
- exhaustiveness checking
- maybe: make it easily swapable for other pattern matching lib
- maybe: make it support binary matching like Erlang/Elixir
Research
It would be useful to research a bit more Rust, Haskell, OCaml, Python, Ruby, Scala, Erlang, Prolog, Elixir
We can also compare several possible designs like the Python folks did in their preparation for the feature.
Next steps
- Decide on general direction
- Offer 1 or more designs in a more detailed version of the RFC
- Implement
Future directions
Not sure
Disclaimer
I am the author of one of the libs gara, so I might be biased.
pinging @krux02 and @andreaferretti which probably have something to say on the topic