Skip to content

Early version of RFC: Pattern matching ? #245

Closed
@alehander92

Description

@alehander92

Pattern matching for Nim

Pattern matching can be a good addition to the stdlib/fusion. It seems it becomes more mainstream:

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 :

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions