Chaining calls to the binary .. operator

Is there a way I can create a unary operator function that can function like:

using Dates


-> 2022..1..1 == Date(2022,1,1)


I can catch a solution like:

struct I{T}

(..)(x, y) = I(x, y)


But then I have to write this to generate a Date


Is there a more simple way to do this?

The .. operator doesn’t support chaining like that.

One way to define custom constructor syntax is to use a string macro, which lets you parse expressions in any way that you want (completely independent of standard Julia syntax). For example:

julia> macro date_str(s)
           args = Meta.parse.(split(s, '-'))
@date_str (macro with 1 method)

julia> date"2022-4-20"

(Though I’m not convinced that this particular example is much clearer than just typing Date(2022, 4, 20), it certainly seems a lot clearer than 2022..4..20.)

PS. What you are describing is not a unary operator — it is a binary operator .. that you want to chain without using parentheses. I’m not sure why the .. doesn’t support chaining, but many other custom operators do, e.g.

julia> :(3⨳4⨳5)
:((3 ⨳ 4) ⨳ 5)

You can use the ++ operator:

julia> using Dates

julia> ++(x, y, z) = Date(x, y, z)
++ (generic function with 1 method)

julia> 2022++1++1

Thanks for your help.

Yes, It`s really confusing that this operator does not support chaining while some others can.

I find another way to do that:


-> 2022~1~1

First, while .. does not support chainning, so I have change it to ~; Second, It is also confusing that when calling 2022~1~1, It calls ~(1,1) firstly, then calls ~(2022,(1,1)). I thought it would be backward.

Thanks for your help. ++ support any number of arguments, it would be the reason way ++ works while .. does not. I also find another way to do it with not having to use ++:

1 Like