We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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.
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:
1:5
>>> 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.
slice
TypeError
Julia parses 10 .+ 1:5 in a way that's surprising to some users.
10 .+ 1:5
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.
x:y
x:y:z
and just use range(). Getting rid of that syntax would solve the problem, but the short syntax is convenient.
range()
In cases that are potentially ambiguous like 2 + 1:5, require parentheses to disambiguate.
2 + 1:5
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.
1.+2
I like the latter two options- whitespace, parentheses.
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Motivating case
Confusion been reported before on Discourse.
Other languages
Someone in that thread said Julia follows Matlab here.
But
:
binds before+
in R.:
is after+
in Pythonbut there isn't much opportunity for confusion in Python because
1:5
doesn't form an expression by itself:and even if it did, it would probably build a
slice
object, which doesn't have arithmetic special methods so it would give aTypeError
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
andx: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
and @StefanKarpinski said
It's a similar solution to the one taken in #19089 for
1.+2
.I like the latter two options- whitespace, parentheses.
The text was updated successfully, but these errors were encountered: