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

Avoiding operator-precedence confusion in 2 + 1:5 #40077

Open
adkabo opened this issue Mar 17, 2021 · 0 comments
Open

Avoiding operator-precedence confusion in 2 + 1:5 #40077

adkabo opened this issue Mar 17, 2021 · 0 comments
Labels
kind:breaking This change will break code parser Language parsing and surface syntax
Milestone

Comments

@adkabo
Copy link
Contributor

adkabo commented Mar 17, 2021

Motivating case

julia> collect(10 .+ 1:5)
Int64[]

julia> collect(10 .+ (1:5))
5-element Array{Int64,1}:
 11
 12
 13
 14
 15

Confusion been reported before on Discourse.

Other languages

Someone in that thread said Julia follows Matlab here.

But : binds before + in R.

> 10 + 1:5
[1] 11 12 13 14 15

: is after + in Python

>>> range(50)[10 + 1:5]
range(11, 5)

but there isn't much opportunity for confusion in Python because 1:5 doesn't form an expression by itself:

>>> range(50)[10 + (1:5)]
  File "<stdin>", line 1
    range(50)[10 + (1:5)]
                     ^
SyntaxError: invalid syntax

and even if it did, it would probably build a slice object, which doesn't have arithmetic special methods so it would give a TypeError anyway.

What's the problem

Julia parses 10 .+ 1:5 in a way that's surprising to some users.

What can be done

Switch the precedence

Julia could switch the precedence of those two operators. This would cause silent behavior change; I don't think that's a good option, even for 2.0.

Get rid of x:y and x:y:z

and just use range(). Getting rid of that syntax would solve the problem, but the short syntax is convenient.

Require parentheses in ambiguous cases

In cases that are potentially ambiguous like 2 + 1:5, require parentheses to disambiguate.

Disallow whitespace inconsistent with precedence

In the 2018 Discourse thread, @simonbyrne suggested

I like the Fortress idea where whitespace which is inconsistent with precedence is a syntax error: this would have caught the original example here.

and @StefanKarpinski said

Yes, I really like that too. It would be breaking though so it would have to be opt-in or wait until 2.0.

It's a similar solution to the one taken in #19089 for 1.+2.


I like the latter two options- whitespace, parentheses.

@adkabo adkabo changed the title Avoiding operator precedence confusion Avoiding operator-precedence confusion Mar 17, 2021
@adkabo adkabo changed the title Avoiding operator-precedence confusion Avoiding operator-precedence confusion in 2 + 1:5 Mar 17, 2021
@JeffBezanson JeffBezanson added the parser Language parsing and surface syntax label Mar 18, 2021
@brenhinkeller brenhinkeller added the kind:breaking This change will break code label Nov 20, 2022
@brenhinkeller brenhinkeller added this to the 2.0 milestone Nov 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:breaking This change will break code parser Language parsing and surface syntax
Projects
None yet
Development

No branches or pull requests

3 participants