# Splat operator

**URL:** <https://discourse.julialang.org/t/splat-operator/122351>\
**Category:** New to Julia\
**Tags:** splat\
**Created:** [November 6, 2024, 7:39pm UTC](https://discourse.julialang.org/t/splat-operator/122351 "2024-11-06T19:39:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Post date:** [November 6, 2024, 7:39pm UTC](https://discourse.julialang.org/t/splat-operator/122351/1 "2024-11-06T19:39:47Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 6, 2024, 7:44pm UTC](https://discourse.julialang.org/t/splat-operator/122351/2 "2024-11-06T19:44:43Z")

</div>

this doesn’t work right? are you sure you’re running your function?

```julia
julia> function mysum(args...)
           sum=0
           for i in args
           sum+=i
           end
       end
mysum (generic function with 1 method)

julia> mysum([1,2,3])
ERROR: MethodError: no method matching +(::Int64, ::Vector{Int64})
For element-wise addition, use broadcasting with dot syntax: scalar .+ array
The function `+` exists, but no method is defined for this combination of argument types.

```

basically when you have `mysum(args...)`, it now can take any number of arguments, like:

```julia
mysum(1,2)
mysum(1,2,3)

```

you can’t do that if you defined the function as `mysum(args)`

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 6, 2024, 7:50pm UTC](https://discourse.julialang.org/t/splat-operator/122351/3 "2024-11-06T19:50:50Z")

</div>

Right, there _is_ definitely a difference here. Often when used in function definition form, I’d call `...` the “slurp” operator as it collects all args (even 0 and 1-argument calls) into a tuple that you iterate over. If you don’t use the `...` in the definition, you’d just get the argument itself (without getting plopped into a tuple) — and that value may or may not itself be iterable.

You likely have more than one method of `mysum` defined.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 6, 2024, 8:03pm UTC](https://discourse.julialang.org/t/splat-operator/122351/4 "2024-11-06T20:03:39Z")

</div>

> [@martin\_sanchez](#):
>
> `function mysum(args...)`

Ask yourself: When you call `mysum([1,2, 3]...)`, where do the values end up?

From your function definition, they end up inside the variable `args`. And if `args` contains the values 1, 2, and 3, it must be a collection. And if it’s a collection, you can iterate over it.

In a function definition, `args...` _slurps_ up the input values, what else could conceivably happen here?

It’s perhaps a bit confusing that the same syntax is used for slurping and splatting, perhaps one could have used `...args`, for example. Note that Python does the same thing.
