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

Ternary operator in multiple lines #10291

Closed
juliohm opened this issue Feb 23, 2015 · 9 comments
Closed

Ternary operator in multiple lines #10291

juliohm opened this issue Feb 23, 2015 · 9 comments
Labels
status:won't change Indicates that work won't continue on an issue or pull request

Comments

@juliohm
Copy link
Sponsor Contributor

juliohm commented Feb 23, 2015

The parser fails with the ternary operator in two lines:

foo = 1 > 0 ? 1
            : 0

ERROR: syntax: colon expected in "?" expression

@sjkelly
Copy link
Contributor

sjkelly commented Feb 23, 2015

You just need to put the colon on the same line as the question mark:

julia> foo = 1 > 0 ? 1 :
       0
1

@jiahao
Copy link
Member

jiahao commented Feb 23, 2015

Alternatively, you can use parentheses:

julia> foo = (1 > 0 ? 1
      : 0)
1

@juliohm
Copy link
Sponsor Contributor Author

juliohm commented Feb 23, 2015

I mean, this is intended behavior? We really need the parenthesis there?

@IainNZ
Copy link
Member

IainNZ commented Feb 23, 2015

Maybe you could argue that the the parser, having seen a ?, but not a :, should keep looking until it finds one I suppose. I think the current behavior is the C behaviour (but my memory might be shakey)

@juliohm
Copy link
Sponsor Contributor Author

juliohm commented Feb 23, 2015

@IainNZ C allows the break before the semicolon:

int main()
{
    int foo = 1 > 0 ? 1
                    : 0;
}

@jiahao
Copy link
Member

jiahao commented Feb 23, 2015

I think the current behavior is simply what the parser currently gives us.

Related: #6823 #9180

@StefanKarpinski
Copy link
Sponsor Member

There's also been some discussion of allowing just the ? without the : to mean something (forget the issue), although I didn't really care for it myself.

@quinnj
Copy link
Member

quinnj commented Feb 23, 2015

Yeah, I was a bigger fan of allowing single line if-then statements, so you
could do

if x then y

instead of

if x; y; end

On Sun, Feb 22, 2015 at 9:34 PM, Stefan Karpinski notifications@github.com
wrote:

There's also been some discussion of allowing just the ? without the : to
mean something (forget the issue), although I didn't really care for it
myself.


Reply to this email directly or view it on GitHub
#10291 (comment).

@JeffBezanson
Copy link
Sponsor Member

Yes, that is #6823. Even if we don't adopt the x ? y syntax, it's good that we erred on the side of leaving it open instead of allowing every combination of whitespace. I don't see the value of putting the : on the next line.

@JeffBezanson JeffBezanson added the status:won't change Indicates that work won't continue on an issue or pull request label Feb 24, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:won't change Indicates that work won't continue on an issue or pull request
Projects
None yet
Development

No branches or pull requests

7 participants