'Is it possible to change order of operators in Julia?

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.



Solution 1:[1]

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.

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bogumił Kamiński
Solution 2 Przemyslaw Szufel