-
Notifications
You must be signed in to change notification settings - Fork 23
Description
originally proposed in nim-lang/Nim#16924 (comment)
proposal 1
we defined dot like operators as operators starting with ., but excluding operators starting with .. like ..<, ..^ etc.
Dot like operators shall have same precedence as .
example
when true:
import macros
macro deb(a: untyped): untyped = echo a.lispRepr
deb a.b.c # parsed as: (a.b) . c
deb a.b.?c # parsed as: (a.b) .? c
deb a.?b.c # parsed as: a .? (b.c) currently; parsed as `(a .? b).c` under this RFCcurrent output:
(DotExpr (DotExpr (Ident "a") (Ident "b")) (Ident "c"))
(Infix (Ident ".?") (DotExpr (Ident "a") (Ident "b")) (Ident "c"))
(Infix (Ident ".?") (Ident "a") (DotExpr (Ident "b") (Ident "c")))
under this RFC, a.?b.c would instead parse as: (a.?b).c, and .? would then be a valid replacement for user-defined ., in particular for jsffi or nimpy, without having to mess around with builtin .:
let j = (a: 1, b: 2).toJson
let foo = j.?bar.?baznote
this reduces the need for dotOperators (see also nim-lang/Nim#16996 which removed dotOperators in std/wrapnils) but this RFC isn't about deprecating dotOperators (this can be discussed elsewhere); in particular, there may be valid remaining use cases for type punning.
After this RFC, jsffi/nimpy + any other library/API that was using dotOperators as a means to provide dynamic field access would then be encouraged to use .? (or similar) instead of ..
This would sidestep issues like nim-lang/Nim#7777, nim-lang/Nim#15607, nim-lang/Nim#13063 for those use cases.
proposal 2 (on top of proposal 1)
all of https://nim-lang.github.io/Nim/manual_experimental.html#special-operators would apply to dot-like operators, except that the flag {.experimental: "dotOperators".} would not be needed for those (except for . itself).
in particular, these can be defined:
template `.?`(a, b): untyped
template `.?()`(a, b): untyped
template `.?=`(a, b, c): untyped(ditto with replacing .? by another dot like operator, eg .!, .$$ etc)
breaking change discussion
- proposal 1 is a breaking change but it's unclear whether this is a problem in practice, as this is probably how user would expect it to work, and IIRC dot like operators are rare/nonexistant; data welcome. A PR implementing this could assess damage on important_packages; a migration strategy could be possible, by controlling this via a scopable pragma, possibly returning warnings in places where the parser change would change semantics and prompt for adding parens to enforce old associativity
- this parser change should be backported to 1.0 branch so that libraries involving dot like operators can work there too
links
- https://nim-lang.github.io/Nim/manual_experimental.html#special-operators-dot-operators
- misc dotOperators · Issue #582 · timotheecour/Nim
- Force experimental switch to be able to use call/dot operators by hlaaftana · Pull Request #16924 · nim-lang/Nim
- see also D's approach, using
opDispatchhttps://tour.dlang.org/tour/en/gems/opdispatch-opapply