-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pattern matching to nim stdlib #17047
Conversation
Converted to draft until syntax |
I've realized that current implementation can be improved in certain areas, and regardless whether we would get it into stdlib at some point, it would differ from this PR (since it also should make use of view types, "let expressions" etc.). Also, I'm much more inclined to support |
Maybe implement it in the compiler directly then, I like |
Why implement it directly in the compiler when it can clearly be done using a macro? I don't think "case looks better" is a valid justification. More - I can use keyword In addition to that - my close was prompted by a code example I made in discord. Someone asked whether nim can match AST in patterns directly, using macro withmatch(head: untyped): untyped =
matchAst head:
of @lhs + @rhs:
echo "matched addition"
of @lhs - @rhs:
echo "matched substraction"
of @head[@body]:
echo "matched array access with one element"
of @head[@head1, @head2]:
echo "matched array access with two elements"
of @call(@head), @call():
echo "matched call with arguments"
if head.isSome():
echo "had one argument" |
If it's builtin, it avoids the problem of "In addition to consolidating syntax from 6+ different libraries (how many should we get until it becomes a real issue of choosing pattern matching library?)" It seems you changed your mind completely on this topic. Which is not a bad thing, but then please be honest. ;-) |
I don't follow how adding it as built-in compiler feature is considered consolidation while macro in stdlib isn't. I haven't changed my mind in the slightest, and I never said I want it as compiler built-in. Macro in the stdlib - yes, and preferably without relying on fragile and implicit overloading of case. I said it in the RFC for case renames (nim-lang/RFCs#332 (comment) "If those two are fixed, I would prefer customCase too, instead of caseStmtMacros"), I say the same thing now. |
I closed this PR because it think I've come up with much better design, so this +5k sloc does not make much sense anymore. |
The only part of my "honest" opinion that changed - it seems like fusion matching worked out just fine, at least for me, and while I would still prefer it to be a part of stdlib I personally don't care enough anymore to push for that if it would mean fighting with "let's make it compiler built-in instead". |
I would say that |
It wouldn't mean that, it's just something I brought up. |
The whole idea behind nim-lang/RFCs#245 was that we have quite a number of pattern-matching libraries, and it would be a good idea to create a more or less standard implementation that would be bundled with nim by default, giving us pattern matching support in a language, by default, shipped with standard installation. But now, due to #16925 this is no longer the case, so I feel it is only logical to add
matching
to standard library directly.From my perspective it is quite clear that if ecosystem has ~9 different libraries implementing partially overlapping features (and in case of
gara/macroutils/nimtrs/patty/matsuri
ordefinesugar/unpack/nimpylib
(partially) - significantly overlapping) it is a sign that there is a demand for pattern matching.In addition to consolidating syntax from 6+ different libraries (how many should we get until it becomes a real issue of choosing pattern matching library?) this RFC also provides support for rust-like if let, makes nim one step closer to being appealing for functional programmers, simplifies two very common use cases (writing macros and extracting/validating data from external sources), makes python-like sequence unpacking possible (there are two libraries for it already).
An increasing number of programming languages implement pattern matching support, and there is a big difference between something being implemented as a third-party module and being shipped with stdlib. Latter one is considered part of the language while the former is just "well, yeah, now I need to install the package manager, find out how to make it work, install the package itself etc."
Taken from this comment [almost] verbatim as nothing has changed in the ecosystem, so everything holds true still
Related