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

Syntax ambiguity when range is used in ternary operator #23599

Closed
AzamatB opened this issue Sep 6, 2017 · 6 comments
Closed

Syntax ambiguity when range is used in ternary operator #23599

AzamatB opened this issue Sep 6, 2017 · 6 comments
Labels
parser Language parsing and surface syntax

Comments

@AzamatB
Copy link
Contributor

AzamatB commented Sep 6, 2017

The following code produces unexpected output:

julia> true ? 1:3 : 4:7
1

of course, it can be fixed by adding braces:

julia> true ? (1:3) : (4:7)
1:3

However, I still think it should be deprecated for the same reason that
1.+2 was deprecated. Especially, in light of the fact that it produces erroneous output.

@ararslan ararslan added the parser Language parsing and surface syntax label Sep 6, 2017
@ararslan
Copy link
Member

ararslan commented Sep 6, 2017

This is actually expected, since ternaries are parsed more eagerly than ranges. You can see what's actually happening by inspecting the output of parse:

julia> parse("true ? 1:3 : 4:7")

WARNING: deprecated syntax "true ? 1:".
Use "true ? 1 :" instead.

WARNING: deprecated syntax "true ? 1 :3".
Use "true ? 1 : 3" instead.
:(if true
        1
    else
        3:4:7
    end)

That is, the precedence is (true ? 1 : (3:4:7)).

@ararslan ararslan closed this as completed Sep 6, 2017
@AzamatB
Copy link
Contributor Author

AzamatB commented Sep 6, 2017

Which version is this? I don't get these warnings in 0.6.

@GregPlowman
Copy link
Contributor

I'm not sure it's erroneous output, but rather a case of parsing precedence.
Presumably it is parsed as:
true ? 1 : (3:4:7)
which is valid syntax.

@ararslan
Copy link
Member

ararslan commented Sep 6, 2017

The output I posted was from the current master, but the parsing is the same across all Julia versions (or at least 0.3-0.7—I don't have 0.2 or lower installed). The deprecation warnings are new though, recent 0.7.

@AzamatB
Copy link
Contributor Author

AzamatB commented Sep 6, 2017

Can you confirm that these warnings don't show up in 0.6?

@StefanKarpinski
Copy link
Sponsor Member

Yes – as a side effect of requiring space after ? in ternary on master, we've also required spaces around the : in ternary, so this is now already a warning, forcing parens or spaces matching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parser Language parsing and surface syntax
Projects
None yet
Development

No branches or pull requests

4 participants