JuliaLang/julia

ternary operator with "return" gives a parse error

Open

#9.180 aberto em 27 de nov. de 2014

Ver no GitHub
 (5 comments) (0 reactions) (0 assignees)Julia (5.773 forks)batch import
help wantedparser

Métricas do repositório

Stars
 (48.709 stars)
Métricas de merge de PR
 (Mesclagem média 20d 6h) (157 fundiu PRs em 30d)

Description

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

Guia do colaborador