Ternary operator and range in Julia

Viewed 55

I went to this error by chance which I fixed with some diverted way, but still I was curious how it is solved for Julia.

Suppose the MWE:

julia> a = 1 ; b = 5 ; some_logical_value = true

julia> @assert a > 0 ? b in 1:10 : some_logical_value
ERROR: syntax: space required before colon in "?" expression
Stacktrace:
 [1] top-level scope at none:1

Which yields an error as, not surprisingly : from ternary operator and : from 1:10 is misleading for Julia. How should we do so?

1 Answers

You just need to add parentheses enclosing the range expression, so either of

@assert a > 0 ? b in (1:10) : some_logical_value

or

@assert a > 0 ? (b in 1:10) : some_logical_value

should work.

Related