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

Proposal: Require whitespace on both sides of - or neither #7399

Closed
SpexGuy opened this issue Dec 11, 2020 · 7 comments · Fixed by #11156
Closed

Proposal: Require whitespace on both sides of - or neither #7399

SpexGuy opened this issue Dec 11, 2020 · 7 comments · Fixed by #11156
Labels
accepted This proposal is planned. breaking Implementing this issue could cause existing code to no longer compile or have different behavior. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@SpexGuy
Copy link
Contributor

SpexGuy commented Dec 11, 2020

This may be out of scope for the language, but it's in line with other accepted proposals like #35 and #114 which aim to avoid misinterpretation.
Here's an example of the problem this prevents:

const directions = [_]isize{
    -pitch-1,
    -pitch,
    -pitch+1
    -1,
    1,
    pitch-1,
    pitch,
    pitch+1,
};

See the bug? It took me a minute.

View Answer Here the `,` is missing from the third item in the list. This makes the list 7 items long instead of 8, and the third item `-pitch+1-1`. If you look closely, you can see that GitHub's syntax highlighting parser actually identifies the bug, and gives the `-` on line 4 a different color to indicate that it's not unary.

My proposal to solve this is to require whitespace either both before and after -, or neither before nor after. We may also want to apply this to other operators for consistency, but I think - is the one that really causes problems. Here are some examples under this rule:

_ = a - b; // fine
_ = a-b; // fine
_ = a -b; // error
_ = a- b; // error
_ = some_really_long_thing -
    some_other_really_long_thing; // ok
_ = some_really_long_thing
    - some_other_really_long_thing; // ok
_ = some_really_long_thing-
    some_other_really_long_thing; // error
_ = some_really_long_thing
    -some_other_really_long_thing; // error
@andrewrk andrewrk added accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. labels Dec 11, 2020
@andrewrk andrewrk added this to the 0.9.0 milestone Dec 11, 2020
@andrewrk andrewrk added the contributor friendly This issue is limited in scope and/or knowledge of Zig internals. label Dec 11, 2020
@SpexGuy
Copy link
Contributor Author

SpexGuy commented Dec 11, 2020

That was fast 😄
Is the accepted version to apply this to all binary operators or just -?

@andrewrk
Copy link
Member

That was fast smile

Yep! Easiest way to get a proposal accepted is to

(1) solve a footgun
(2) make the language accept fewer permutations of source code rather than more

Is the accepted version to apply this to all binary operators or just -?

I'd say all, for consistency, but feel free to make a case for just -.


Tasks to close the issue:

  • Update the language spec / grammar
  • Stage1 parser
  • Stage2 parser

@ghost
Copy link

ghost commented Dec 11, 2020

I have a question related to this and #35, is zig fmt expected to be able to run on code that doesn't compile? Would zig fmt help to "fix" bad whitespace, or will it just die because the code doesn't compile?

@frmdstryr
Copy link
Contributor

Linear / polynomial equations are often written using + to make the coefficients clear...

So what about x*x + -2*x + c0? Is that invalid?

@SpexGuy
Copy link
Contributor Author

SpexGuy commented Dec 12, 2020

That's fine, the * operators have whitespace on neither side and the + operators have whitespace on both sides. Unary - is not affected by this rule.

@andrewrk
Copy link
Member

I have a question related to this and #35, is zig fmt expected to be able to run on code that doesn't compile? Would zig fmt help to "fix" bad whitespace, or will it just die because the code doesn't compile?

No, zig fmt would fail to parse it. If it "fixed" it then it would re-introduce the footgun.

@SpexGuy SpexGuy added the breaking Implementing this issue could cause existing code to no longer compile or have different behavior. label Dec 31, 2020
@PavelVozenilek
Copy link

PavelVozenilek commented Jan 7, 2021

If all operators are required to have mandatory space around, it would increase readability and also allow to use - (minus) as name character. this-style may feel more readable than this_style.

Where such requirement would really shine are long names: some_long_name-other_long_name vs some_long_name - other_long_name or even some-long-name - other-long-name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. breaking Implementing this issue could cause existing code to no longer compile or have different behavior. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants