.. (two dots) as a operator name

IntervalSets.jl defines .. as a interval operator. I tried using ... as an operator but Julia complains that it is an invalid identifier name. This means that .. is treated somewhat special. Where can I find the list of legal operators and are there any other special ones, like the ..?

How did you try to define it? It works for me:

julia> ..(a,b) = a:b
.. (generic function with 1 method)

julia> 3..4
3:4

What error exactly do you get for your code?

Two dots work. Try doing that with ... (three dots).

Yes. It’s not .. that’s special, but ..., since it is handled specially during lowering:

julia> Meta.@lower add(xs...) = reduce(+, xs)
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─      $(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─     return $(Expr(:method, :add))
)))
│        $(Expr(:method, :add))
│   %3 = Core.Typeof(add)
│   %4 = Core.apply_type(Vararg, Core.Any)
│   %5 = Core.svec(%3, %4)
│   %6 = Core.svec()
│   %7 = Core.svec(%5, %6, $(QuoteNode(:(#= REPL[2]:1 =#))))
│        $(Expr(:method, :add, :(%7), CodeInfo(
    @ REPL[2]:1 within `none`
1 ─ %1 = reduce(+, xs)
└──      return %1
)))
└──      return add
))))

You can’t override the splatting/slurping operator.

True, I forgot about slurping. It seems that it parses three or more dots as a slurping operator.

Is there a way then of defining .... as an operator or does it need to be hard coded somewhere in Julia’s parser?

I like to survey the parser source code when looking for candidate characters for my own obscure operators. Take note of built-in operator precedences (increasing as you go down) and make sure it won’t surprise you later on.

1 Like