Weird error when trying to use `≔` (\coloneq) as infix operator

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