-
Notifications
You must be signed in to change notification settings - Fork 260
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
[SUGGESTION] Disambiguate postfix unary and binary operators #152
Comments
A better simplification is to ban && in cpp2 and instead require the "and" keyword. Your suggestion is good, but still requires some teaching and can lead to code that requires detailed knowledge to parse (e.g. "x&&&y&" is much harder to parse than "x& and y&")
On 30 November 2022 12:41:55 Sadeq ***@***.***> wrote:
DISCLAIMERS TO SET EXPECTATIONS: I'm generally against language feature requests/changes unless they can be shown to improve simplicity, safety, or toolability in a quantifiable way. So:
* Please limit suggestions to quantifiable improvements to C++ simplicity, safety, or toolability. Quantifiable means that there is some kind of measurable data that helps motivate the change and measure success. Two of the big ones that will get my attention are eliminating vulnerabilities and eliminating guidance, so use those below please.
* Please do not suggest syntax changes. I accept there are hundreds of opinions and everyone will prefer something a little different. Syntax isn't the big thing, fixing semantics is -- reducing concept count, increasing toolability, are the big payoff.
* Please do not suggest things that amount to personal taste. I accept there are hundreds of personal tastes and everyone will prefer something a little different. For example, established stakes in the ground include that this declaration syntax is going to be left-to-right, and it's going to use : for every declaration and only for declarations.
Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code? If yes, please be specific about the classes of bugs that would go away, with an example or two (especially a link to a real CVE or two).
Will your feature suggestion eliminate X% of current C++ guidance literature?" If yes, please be specific about what things we would no longer need to teach/learn or that would be simplified and how, with an example or two (especially a link to a real "Effective C++" or "C++ Core Guidelines" guideline or two). For ideas, you can refer to my CppCon 2020 talk starting at 10:31<https://youtu.be/6lurOCdaj0Y?t=631> where I summarize a categorized breakdown based on over 600 C++ guidance literature rules I cataloged and analyzed.
Describe alternatives you've considered.
There's nearly always more than one way to improve something. What other options did you consider? Why is the one you're suggesting better than those?
Currently in Cpp2:
1. x&&y is equivalent to (x&) & (y), because there is no whitespace before the first symbol &.
2. but x &&y is equivalent to (x) && (y), because there is a whitespace before the first symbol &.
Although x&&y and x &&y are too much similar, but they behave completely different. A typical programmer expects x&&y or x &&y to be equivalent to (x) && (y).
Cpp2 can be simpler with less surprising results, if Cpp2 uses the following rule to disambiguate postfix unary and binary operators:
1. If there is a combination of operators between two identifiers or literals or parenthesis, Cpp2 should check whether the last symbols are a valid binary operator, in a way that Cpp2 will try to find the biggest possible match for the binary operator. e.g. the binary operator for x&&&y will be && (because && is a valid binary operator and it has more symbols than & binary operator).
2. After Cpp2 has found the binary operator, it will treat other symbols as postfix unary operators.
For example:
1. x&&y is equivalent to (x) && (y).
2. x &&y is equivalent to (x) && (y).
3. x & &y is equivalent to (x&) & (y).
4. x& &y is equivalent to (x&) & (y).
—
Reply to this email directly, view it on GitHub<#152>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AALUZQNIBV3CGTUFHISLMLDWK5DRBANCNFSM6AAAAAASPSZWBM>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
This sounds more like a bug in the lexer, not being eager. But it is true that is makes the syntax harder to parse/read than necessary. |
I personally am against using keywords such as Besides, what would you do for binary The issue of postfix |
Thanks, everyone.
I can't reproduce this... maybe I changed something since you opened this issue? But I can't think what it would be because that Maybe you were thinking of Here's what this means for your examples:
I like this because I think it gets rid of the visually ambiguous cases, and the error message is clear. And for
Re I'm open to further improving this. There's a tradeoff here that I cover in the Design note, which I've updated with this updated current resolution. Thanks! |
Thank you. Yes, I were thinking of Your solution gets rid of visually ambiguous cases very well (now both |
Before this I had been trying out making `a*b` work without whitespace by making `a****b` interpret the final `*` as binary if followed by something that wouldn't make sense if it was unary, but that's a bit subtle. (And similarly for `&` and should also include `~`) See additional discussion in hsutter#152.
Update: I decided to revert the weird See longer note in #989. |
Currently in Cpp2:
x&&y
is equivalent to(x&) & (y)
, because there is no whitespace before the first symbol&
.x && y
is equivalent to(x) && (y)
, because there is a whitespace before the first symbol&
.Although
x&&y
andx && y
are too much similar in the syntax, but they have completely different results. A typical programmer expectsx&&y
andx && y
to be equivalent to(x) && (y)
.Cpp2 can be simpler with less surprising results, less programmer responsibility to care about the syntax, if Cpp2 uses the following rule to disambiguate postfix unary and binary operators:
x&&&y
will be logical&&
operator (because&&
is a valid binary operator and also it has more symbols than binary&
operator).For example:
x&&y
is equivalent to(x) && (y)
.x && y
is equivalent to(x) && (y)
.x & & y
is equivalent to(x&) & (y)
.x& & y
is equivalent to(x&) & (y)
.The text was updated successfully, but these errors were encountered: