Julia ternary operator without `else`

4

Consider the ternary operator in Julia

julia> x = 1 ; y = 2

julia> println(x < y ? "less than" : "not less than")
less than

Question: Is there a way to omit the : part of the statement ? Something which would be equivalent to

if condition
    # dosomething
end

without writing that if the condition is not met, nothing should be done.

NB: I researched the answer but nothing came out, even in related questions (1, 2)

Share
3
  • I don't know Julia, but if you don't want an else body, you don't want a ternary (in every language I know). The purpose of a ternary is to evaluate to one of two values based on a condition. If you want to run side effects, don't use a ternary (conditional expression). – Carcigenicate Apr 13 at 14:05
  • Thanks @Carcigenicate, maybe I shouldn't use the ternary operator in that case indeed. – Joris Limonier Apr 13 at 14:10
  • FWIW, "ternary" means "composed of 3 parts"... So the question sounds a bit like "Can I have a 3-part thing without the 3rd part?" – Benoit Pasquier Apr 13 at 22:00

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue