Ternary operator and range in Julia


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
 [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?


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

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


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

should work.

Answered By – Simeon Schaub

Answer Checked By – Willingham (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.