Skip to content
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

Change command + = syntax #442

Closed
metagn opened this issue Jan 13, 2022 · 6 comments · Fixed by nim-lang/Nim#20994
Closed

Change command + = syntax #442

metagn opened this issue Jan 13, 2022 · 6 comments · Fixed by nim-lang/Nim#20994

Comments

@metagn
Copy link
Contributor

metagn commented Jan 13, 2022

Currently this:

foo a = b

parses as this:

foo(a) = b

The proposal is to make this mean (only in statement form):

foo(a = b)

foo a = b, c = d syntax should be an easy outcome of this for a reason explained below.

Reasoning:

  • Command syntax generally consumes all infixes on its right, such as in a b + c, or even a b += c. Since = forms an expression in named argument syntax, it makes sense to consider it equivalent to an infix operation.
  • foo a, b = c already parses as foo(a, b = c) (not in the grammar). Hence foo a = b, c = d should be easy to support.
  • I can't think of a justification for the current syntax. foo(a) = b or a.foo = b are already possible and not much less ergonomic.
  • Other languages with command syntax, such as Ruby, already employ this proposed syntax, which just makes Nim's current syntax weirder.
  • I've never seen the current syntax in code likely because of the above problems.
  • Named arguments in Nim are used like foo(a = b, c = d), so there is a clear use for this syntax in command form. Macros and DSLs would also benefit from this, for example unpack (a, (b, c)) = (1, (2, 3)) would become possible.

This change would definitely break code and macros which use the current syntax. However I genuinely have never seen any code which does, the workaround is simple and all the capabilities of the old syntax seem to be kept. And it can be easily tied to a big, backwards-incompatible release like 2.0.

From what I can think of, this would require some redundancy in the grammar, but there may still be simple ways to include it. It shouldn't cause a giant ambiguity anyway.

Edit: Looking more into the parser, I am changing this proposal to only work in statement form.

Edit: I have found that my own macro library has tests using the old syntax. Thankfully it does nothing more than create an inconsistency with the other macros. It can also easily still support the old invocations. I did not think about how this could affect existing macros rather than just normal code, but AST has been noticeably changed before and in my view no information is lost here.

Edit 2: Old issue about this I didn't see: nim-lang/Nim#15050

@mratsim
Copy link
Collaborator

mratsim commented Jan 13, 2022

That would break the rule "unary binds stronger than binary". It's an easy enough rule to remember, I'm not sure the exception is worth it. And later all parsing/syntax highlighting packages need to be updated.

@metagn
Copy link
Contributor Author

metagn commented Jan 13, 2022

That would break the rule "unary binds stronger than binary".

But that rule already doesn't apply for a b + c, or even a b += c or a b := c. = is the exception here.

And later all parsing/syntax highlighting packages need to be updated.

I doubt they parse this correctly already. Even if they didn't, I don't think they would break that much. But yes, this is a breaking change overall.

@Araq
Copy link
Member

Araq commented Jan 13, 2022

I like the idea.

metagn added a commit to metagn/Nim that referenced this issue Jan 13, 2022
Types are separated from expressions and better reflected in the grammar.
@c-blake
Copy link

c-blake commented Jan 18, 2022

It may be worth observing here that named arguments already work for non-first params:

proc foo(x: int, a=2, b=3) = discard
foo 1, b=2, a=3

@metagn
Copy link
Contributor Author

metagn commented Jan 18, 2022

Was mentioned but has been included more thoroughly in the proposal. In my opinion that is in favor of the proposed syntax.

@c-blake
Copy link

c-blake commented Jan 18, 2022

I agree "all arguments working" is a good consistency principle. It took me a while to discover that it worked for non-first args, actually. Sorry if I didn't notice the mention already. It may be the strongest pro-argument here, but I think it would be good for other Nimmers to weigh in.

You may also be interested in what I call "named do" which I think of as closely related - making named parameter passing work in "code block notation" as well as "code expression notation" (which already works). You just need a way to connect the parameter name and foo.do: or do(foo): seem natural fits.

metagn added a commit to metagn/Nim that referenced this issue Jul 10, 2022
Types are separated from expressions and better reflected in the grammar.
metagn added a commit to metagn/Nim that referenced this issue Jul 20, 2022
Types are separated from expressions and better reflected in the grammar.
metagn added a commit to metagn/Nim that referenced this issue Sep 3, 2022
Types are separated from expressions and better reflected in the grammar.
metagn added a commit to metagn/Nim that referenced this issue Sep 3, 2022
Types are separated from expressions and better reflected in the grammar.
metagn added a commit to metagn/Nim that referenced this issue Sep 7, 2022
Types are separated from expressions and better reflected in the grammar.
metagn added a commit to metagn/Nim that referenced this issue Sep 17, 2022
Types are separated from expressions and better reflected in the grammar.
metagn added a commit to metagn/Nim that referenced this issue Dec 2, 2022
Types are separated from expressions and better reflected in the grammar.
Araq pushed a commit to nim-lang/Nim that referenced this issue Dec 6, 2022
* Breaking parser changes, implement nim-lang/RFCs#442

Types are separated from expressions and better reflected in the grammar.

* add test

* more accurate grammar

* fix keyword typedescs

* accept expressions in proc argument lists

* CI "fixes"

* fixes

* allow full ref expressions again, adapt old tests

* cleanup, fix some tests

* improve grammar, try and revert semtypes change

* restrict sigil binding to identOrLiteral

* fix, should have caught this immediately

* add changelog entry, fix double not nil bug

* correct grammar

* change section

* fix

* real fix hopefully

* fix test

* support LL(1) for tuples

* make grammar.txt too
survivorm pushed a commit to survivorm/Nim that referenced this issue Feb 28, 2023
* Breaking parser changes, implement nim-lang/RFCs#442

Types are separated from expressions and better reflected in the grammar.

* add test

* more accurate grammar

* fix keyword typedescs

* accept expressions in proc argument lists

* CI "fixes"

* fixes

* allow full ref expressions again, adapt old tests

* cleanup, fix some tests

* improve grammar, try and revert semtypes change

* restrict sigil binding to identOrLiteral

* fix, should have caught this immediately

* add changelog entry, fix double not nil bug

* correct grammar

* change section

* fix

* real fix hopefully

* fix test

* support LL(1) for tuples

* make grammar.txt too
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
* Breaking parser changes, implement nim-lang/RFCs#442

Types are separated from expressions and better reflected in the grammar.

* add test

* more accurate grammar

* fix keyword typedescs

* accept expressions in proc argument lists

* CI "fixes"

* fixes

* allow full ref expressions again, adapt old tests

* cleanup, fix some tests

* improve grammar, try and revert semtypes change

* restrict sigil binding to identOrLiteral

* fix, should have caught this immediately

* add changelog entry, fix double not nil bug

* correct grammar

* change section

* fix

* real fix hopefully

* fix test

* support LL(1) for tuples

* make grammar.txt too
bung87 pushed a commit to bung87/Nim that referenced this issue Jul 29, 2023
* Breaking parser changes, implement nim-lang/RFCs#442

Types are separated from expressions and better reflected in the grammar.

* add test

* more accurate grammar

* fix keyword typedescs

* accept expressions in proc argument lists

* CI "fixes"

* fixes

* allow full ref expressions again, adapt old tests

* cleanup, fix some tests

* improve grammar, try and revert semtypes change

* restrict sigil binding to identOrLiteral

* fix, should have caught this immediately

* add changelog entry, fix double not nil bug

* correct grammar

* change section

* fix

* real fix hopefully

* fix test

* support LL(1) for tuples

* make grammar.txt too
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants