When overloading ≔
like this:
julia> ≔(a, b) = a==b
≔ (generic function with 1 method)
the following works just fine:
julia> ≔(3,2)
false
But I get a weird error when trying to use it as an infix-operator:
julia> 2≔3
ERROR: syntax: invalid syntax (≔ 2 3)
Is this even supposed to work? The error message looks more like a bug in Julia’s Lisp parser or maybe I’m missing something?
The list of symbols recognized as infix operators (and associated precedence) can be found in the Julia parser source code. For the most part, this list is a subset of unicode category Sm(Symbol, math).
At the moment, it includes for example:
parsed with the same precedence as + (addition):
+ - ⊕ ⊖ ⊞ ⊟ ∪ ∨ ⊔ ± ∓ ∔ ∸ ≏ ⊎ ⊻ ⊽ ⋎ ⋓ ⧺ ⧻ ⨈ ⨢ ⨣ ⨤ ⨥ ⨦
⨧ ⨨ ⨩ ⨪ ⨫ ⨬ ⨭ ⨮ ⨹ ⨺ ⩁ ⩂ ⩅ ⩊ ⩌ ⩏ ⩐ ⩒ ⩔ ⩖ ⩗ ⩛ ⩝ ⩡ ⩢ ⩣
parsed with the same precedence as * (multiplication):
* / ÷ % & ⋅ ∘ × ∩ ∧ ⊗ ⊘ ⊙ ⊚ ⊛ ⊠ ⊡ ⊓ ∗ ∙ ∤ ⅋ ≀ ⊼ ⋄ ⋆ ⋇
⋉ ⋊ ⋋ ⋌ ⋏ ⋒ ⟑ ⦸ ⦼ ⦾ ⦿ ⧶ ⧷ ⨇ ⨰ ⨱ ⨲ ⨳ ⨴ ⨵ ⨶ ⨷ ⨸ ⨻
⨼ ⨽ ⩀ ⩃ ⩄ ⩋ ⩍ ⩎ ⩑ ⩓ ⩕ ⩘ ⩚ ⩜ ⩞ ⩟ ⩠ ⫛ ⊍ ▷ ⨝ ⟕ ⟖ ⟗
2 Likes
I believe you can only use it as an infix operator in a macro. It is parsed as an infix operator:
julia> dump(:(2 ≔ 3))
Expr
head: Symbol ≔
args: Array{Any}((2,))
1: Int64 2
2: Int64 3
with assignment precedence, but you can’t overload assignment operators. The 1.13 parser gives a more descriptive error:
julia> 3 ≔ 2
ERROR: syntax: unsupported assignment operator "≔"
Stacktrace:
[1] top-level scope
@ REPL[4]:1
However, since it is parsed, you can write a macro that rewrites it into something else. You can do the same thing with :=
.
julia> dump(:(2 := 3))
Expr
head: Symbol :=
args: Array{Any}((2,))
1: Int64 2
2: Int64 3
julia> Base.operator_precedence(:(=))
1
julia> Base.operator_precedence(:(:=))
1
julia> Base.operator_precedence(:(≔))
1
See also Is it possible to overload `:=`?
PS. Whoops, I didn’t realize that this was an ancient thread.
3 Likes