Is it possible to change order of operators in Julia?

Viewed 59

I like to change the order of & programmatically. Is it possible? for example 3 < 4 & 1 < 4, & is evaluated first, but I like it be evaluated last.

2 Answers

You cannot change the precedence of & - precedence is resolved at parse time in Julia. Do either what Colin T Bowers suggested or use && which has a lower precedence than comparison operators.

Actually this is possible if you define an operator with lower precedence than & and give it the meaning of &.

julia> ↺(a,b) = a & b
↺ (generic function with 1 method)

julia> 3 < 4 ↺ 1 < 4
true

This is of course some kind of "cheating" and it will make writing other pieces of code with ↺ awkward because it has now the same precedence level as the => operator.

The list of available operators along with their precedence can be found at: https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm

Related