I'm struggling with the new dot-like operators in Nim 1.6. I wrote a library called questionable that makes heavy use of the .? operator. You can for instance call a function on an optional value like this:
option.?foo(bar)
I match these using a macro with the following signature:
macro `.?`*(option: typed, call: untyped{nkCall}): untyped
When I enable the nimPreviewDotLikeOps option, this macro will no longer be called because instead of parsing the expression above as an infix operation on an identifier and a call, it now parses it as a call to an infix operator:
Before: | After:
|
Infix | Call
Ident ".?" | Infix
Ident "option" | Ident ".?"
Call | Ident "option"
Ident "foo" | Ident "foo"
Ident "bar" | Ident "bar"
I'm at a loss how to write a macro that matches a call and its arguments with the new dot-like operators. Any help would be greatly appreciated :)
Given that the distinction for "dot-like operators" are made, would it make sense for a.?b(c) to transform into:
Infix
Ident ".?"
Ident "a"
Ident "b"
Ident "c"
or maybe a new DotLike node kind instead of Infix? Or just transform Call expressions with dotlike infix expression call addresses into the old AST.
In case you are wondering, Infix expressions with more than 2 arguments are already possible by doing 1 + 2: 3.
If you were to ask me, I would prefer either the DotLike or the old Call options, because they're explicit. The Infix with more arguments would also work for me.
But I have no insight into how the parser works for the dot-like operators, and what is achievable without too much hassle.
Interesting, I didn't see the RFC before. The .?() operator would indeed solve my problem.
By the way: I tried the workaround with the () operator, but I ran into the problem that I cannot differentiate between a function call option.?foo() or a field access option.?foo, and therefore do not know whether to return something that is callable with the () operator, or a simple field value.