Skip to content

parser: make dot operators (eg:.?) same precedence/associativity as . #341

@timotheecour

Description

@timotheecour

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 RFC

current 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.?baz

note

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

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