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

Warn when whitespace is misleading when compared to operator precedence #4339

Closed
rakudrama opened this issue Aug 3, 2012 · 4 comments
Closed
Labels
closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug

Comments

@rakudrama
Copy link
Member

I have not been convinced by some of the puzzlers. They rely to some degree on sleight-of-hand. Consider:

    main() {
      print("A simple sum: 2 + 2 = " +
            2 + 2);
    }

In Java this prints "A simple sum: 2 + 2 = 22".

Why do I say 'sleight-of-hand'? Operator + groups from the left. In the expression a+b+c, the first + binds tighter, giving an evaluation equivalent to (a+b)+c. In the puzzler, there is more whitespace following the first operator, even though it binds tighter due to grouping from the left. The choice of whitespace is misleading in the same way that

print(1 + 2 *
      3 + 4);

might mislead some to think it prints 21.

The 'Animal farm' puzzler does the same:

main() {
    final String pig = "length: 10";
    final String dog = "length: " + pig.length;

    print("Animals are equal: "
          + pig == dog);
}

  • binds tighter than == but is surrounded by more whitespace.

We might be able to do something about misleading whitespace. The editor could flag expressions where the whitespace suggests operator binding that is contrary to the actual binding.

Suggested implementation:
Attach to each binary operation node the whitespace to the left of the operator and to the right of the operator.
Warn when the whitespace in a subtree is looser than the whitespace on the binary operation node.
One whitespace string is looser than another if it has more newlines, or if it has more spaces when both have no newlines.

Although Dart no longer has + for strings, this is still a useful warning - see Issue #114 for a precedence issue inherited from C/C++/Java that has not been fixed.

@danrubel
Copy link

danrubel commented Aug 3, 2012

Added this to the Later milestone.

@kasperl
Copy link

kasperl commented Jul 10, 2014

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

@sethladd
Copy link
Contributor

Added NotPlanned label.

@clayberg
Copy link

Added this to the 1.6 milestone.

@rakudrama rakudrama added Type-Enhancement closed-not-planned Closed as we don't intend to take action on the reported issue labels Aug 13, 2014
@rakudrama rakudrama added this to the Oldschool-Later milestone Aug 13, 2014
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants