# How to define an infix operator that can act on multiple arguments at once like \`:\`?

**URL:** https://discourse.julialang.org/t/how-to-define-an-infix-operator-that-can-act-on-multiple-arguments-at-once-like/64954
**Category:** General Usage
**Tags:** question, infix
**Created:** [July 19, 2021, 10:24pm UTC](https://discourse.julialang.org/t/how-to-define-an-infix-operator-that-can-act-on-multiple-arguments-at-once-like/64954 "2021-07-19T22:24:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [July 19, 2021, 10:24pm UTC](https://discourse.julialang.org/t/how-to-define-an-infix-operator-that-can-act-on-multiple-arguments-at-once-like/64954/1 "2021-07-19T22:24:16Z")

</div>

Forgive me if I am wrong, but why can `:` act on 3 arguments at a time as an infix operator?

```julia
julia> @which 1:2:3
(::Colon)(start::T, step::T, stop::T) where T<:Real in Base at range.jl:22

```

If I want to define a new operator `→`, which does something the changes a global variable (for simplicity, I will just use `+` in my examples) but returns `nothing`, I cannot make it act like `:`:

```julia
→(a, b) = (a + b; nothing)
→(a, b, c, xs...) = (sum([a, b, c, xs...]); nothing)

julia> 1 → 2 → 3
ERROR: MethodError: no method matching +(::Int64, ::Nothing)
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at operators.jl:560
  +(::T, ::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} at int.jl:87
  +(::Union{Int16, Int32, Int64, Int8}, ::BigInt) at gmp.jl:534
  ...
Stacktrace:
 [1] →(a::Int64, b::Nothing)
   @ Main ./REPL[4]:1

```

That is, `1 → 2 → 3` is treated as `(1 → 2) → 3`, but `1 → 2` returns nothing, which hinders the `→ 3` operation. But this can work:

```julia
julia> →(1, 2, 3, 4)

```

How can I write my code to let the `→` operate on my entire collection (1,2,3,4) but not iteratively?

---

<div class="post-metadata">

### Author: ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)
#### Post date: [July 19, 2021, 11:09pm UTC](https://discourse.julialang.org/t/how-to-define-an-infix-operator-that-can-act-on-multiple-arguments-at-once-like/64954/2 "2021-07-19T23:09:14Z")

</div>

You can’t.  
Not with that symbol.  
`+`, `*` and `++` can have any number of arguments.  
`:` can have 2 or 3  
`-` can have 1 or 2.

Everything else is either 1 only or 2 only.

But iirc you can and unicode sub/superscript modifiers to any of the ones mentioned to get a new operator that has the same variadicity as original form

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [July 19, 2021, 11:18pm UTC](https://discourse.julialang.org/t/how-to-define-an-infix-operator-that-can-act-on-multiple-arguments-at-once-like/64954/3 "2021-07-19T23:18:15Z")

</div>

> [@oxinabox](#):
>
> `+` and `*` can have any number of arguments.

And `++` as well.

---

<div class="post-metadata">

### Author: ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)
#### Post date: [July 19, 2021, 11:21pm UTC](https://discourse.julialang.org/t/how-to-define-an-infix-operator-that-can-act-on-multiple-arguments-at-once-like/64954/4 "2021-07-19T23:21:53Z")

</div>

But you can maybe fake it

```julia
struct Part
    a
    b
end

→(a,b) = Part(a,b)
→(x::Part, c) = foo(x.a, x.b, c)

foo(a,b,c) = ...

```

You could also write a macro to rewrite a line, but I will let someone to else demo that as it is 💤
