JuliaLang/julia

ternary operator with "return" gives a parse error

Open

#9180 aperta il 27 nov 2014

Vedi su GitHub
 (5 commenti) (0 reazioni) (0 assegnatari)Julia (5773 fork)batch import
help wantedparser

Metriche repository

Star
 (48.709 star)
Metriche merge PR
 (Merge medio 20g 6h) (157 PR mergiate in 30 g)

Descrizione

As discussed here: https://groups.google.com/forum/?fromgroups=#!topic/julia-users/9Ps2xYWSWMQ julia parses ternary operator with return not correct (or at least not intuitive).

function bar(x)
    x > 10 ? return(0) : nothing
    2
end

and

function bar(x)
    x > 10 ? return 0 : nothing
    2
end

result in "ERROR: syntax: colon expected in "?" expression".

The versions below work as expected.

function foo(x)
    x <= 10 ? nothing : return(0)
    2
end

or that

function foo(x)
    x > 10 ? (return 0) : nothing
    2
end

Guida contributor