7 ms·
Yes. ADT are case objects: they are widely used. https://nim-lang.org/docs/tut2.html#object-oriented-programming-object-variants https://nim-lang.org/docs/tut
by alehander42 8y ago
Yes.
ADT are case objects: they are widely used.
https://nim-lang.org/docs/tut2.html#object-oriented-programming-object-variants https://nim-lang.org/docs/tut2.html#object-oriented-programm...
There are some macros for easier generation: https://github.com/andreaferretti/patty#constructing-variant-objects https://github.com/andreaferretti/patty#constructing-variant...
There are pattern matching libs: pattern matching is implemented on top of metaprogramming.
General pattern matching:
the original https://github.com/andreaferretti/patty https://github.com/andreaferretti/patty
https://github.com/alehander42/gara https://github.com/alehander42/gara (disclaimer: my lib)
patty and gara should eventually combine into a single lib: including probably the current gara matching DSL(inspired by patty) and patty's variant DSL in the same place.
AST pattern matching:
optimized for macros, nice error reporting https://github.com/krux02/ast-pattern-matching https://github.com/krux02/ast-pattern-matching
- andrenth 8y agoCool. Do the pattern matching libraries check for exhaustiveness in the match cases? Do you see this eventually becoming part of the language?
- dom96 8y ago> Do the pattern matching libraries check for exhaustiveness in the match cases? I'm not 100% sure, but I would guess yes. > Do you see this eventually becoming part of the language? I can answer this with some confidence: no. Nim is designed to have a small but very extensible core via metaprogramming. This is why we encourage users to write such macros. They may end up in our stdlib eventually but it's unlikely they'll be part of the language.
- alehander42 8y agoAt least gara can't check exhaustiveness now: require one to add `else` to check all cases: it might be harder in general make sure branches match each possible value. However it should be possible for sum types/enums: `case` already does it