# Chaining calls to the binary .. operator

**URL:** <https://discourse.julialang.org/t/chaining-calls-to-the-binary-operator/83219>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 23, 2022, 1:34am UTC](https://discourse.julialang.org/t/chaining-calls-to-the-binary-operator/83219 "2022-06-23T01:34:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [June 23, 2022, 1:34am UTC](https://discourse.julialang.org/t/chaining-calls-to-the-binary-operator/83219/1 "2022-06-23T01:34:46Z")

</div>

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

```julia
using Dates

..(x::Int,y::Int,z::Int)=Date(x,y,z)

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

true

```

I can catch a solution like:

```julia
struct I{T}
    x::T
    y::T
end

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

(..)(x::I{Int},y::Int)=I(x.x,x.y,y)

```

But then I have to write this to generate a `Date`

```julia
(2022..1)..1

```

Is there a more simple way to do this?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 23, 2022, 1:47am UTC](https://discourse.julialang.org/t/chaining-calls-to-the-binary-operator/83219/2 "2022-06-23T01:47:36Z")

</div>

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
julia> macro date_str(s)
           args = Meta.parse.(split(s, '-'))
           :(Date($args...))
       end
@date_str (macro with 1 method)

julia> date"2022-4-20"
2022-04-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](https://en.wikipedia.org/wiki/Unary_operation) — 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
julia> :(3⨳4⨳5)
:((3 ⨳ 4) ⨳ 5)

```

---

<div class="post-metadata">

**Author:** ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)\
**Post date:** [June 23, 2022, 1:52am UTC](https://discourse.julialang.org/t/chaining-calls-to-the-binary-operator/83219/3 "2022-06-23T01:52:16Z")

</div>

You can use the `++` operator:

```julia
julia> using Dates

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

julia> 2022++1++1
2022-01-01

```

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [June 23, 2022, 2:16am UTC](https://discourse.julialang.org/t/chaining-calls-to-the-binary-operator/83219/4 "2022-06-23T02:16:32Z")

</div>

Thanks for your help.

> [@stevengj](#):
>
> I’m not sure why the `..` doesn’t support chaining

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

I find another way to do that:

```julia
Base.:(~)(x,y)=I(x,y)
Base.:(~)(year_num::Int,i::I{Int})=Date(year_num,i.x,i.y)

-> 2022~1~1
2022-01-01

```

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.

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [June 23, 2022, 2:21am UTC](https://discourse.julialang.org/t/chaining-calls-to-the-binary-operator/83219/5 "2022-06-23T02:21:09Z")

</div>

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 `++`:

> [@Brian1](#):
>
> ```julia
> Base.:(~)(x,y)=I(x,y)
> Base.:(~)(year_num::Int,i::I{Int})=da(year_num,i.x,i.y)
> 
> -> 2022~1~1
> 2022-01-01
> 
> ```

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [July 1, 2022, 3:26pm UTC](https://discourse.julialang.org/t/chaining-calls-to-the-binary-operator/83219/6 "2022-07-01T15:26:45Z")

</div>

```julia
julia> 2022++1++1
2022-01-01

```

It’s cool (and very surprising to me, possible), but not what I would want people to start using (for `Date`s nor ~). I’m not sure why, but unlike with ++, the example with 2022~1~1 didn’t work for me (in Julia 1.7.0 or 1.9):

```julia
julia> Base.:(~)(year_num::Int,i::I{Int})=Date(year_num,i.x,i.y)
ERROR: UndefVarError: I not defined

```

At least the latter is chaining, and whatever you want to call with ++ (chaining or) ternary operator, are there such ternary operator uses, in any Julia code base? For any operator (or just in math, at least very rare).

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [July 1, 2022, 3:57pm UTC](https://discourse.julialang.org/t/chaining-calls-to-the-binary-operator/83219/7 "2022-07-01T15:57:52Z")

</div>

You have to define `I` first.

> [@Brian1](#):
>
> ```julia
> struct I{T}
> x::T
> y::T
> end
> 
> ```
