# Trouble in understanding how to use varargs as a constructor

**URL:** <https://discourse.julialang.org/t/trouble-in-understanding-how-to-use-varargs-as-a-constructor/14262>\
**Category:** New to Julia\
**Created:** [August 29, 2018, 7:35pm UTC](https://discourse.julialang.org/t/trouble-in-understanding-how-to-use-varargs-as-a-constructor/14262 "2018-08-29T19:35:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wsrinin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsrinin/32/7676_2.png) [@wsrinin](https://discourse.julialang.org/u/wsrinin)\
**Post date:** [August 29, 2018, 7:35pm UTC](https://discourse.julialang.org/t/trouble-in-understanding-how-to-use-varargs-as-a-constructor/14262/1 "2018-08-29T19:35:55Z")

</div>

Hi, I have trouble understanding the bolded line following code from RecursiveArrayTools.jl.

```julia
struct ArrayPartition{T,S&lt;:Tuple} &lt;: AbstractVector{T}
x::S
end
## constructors
**ArrayPartition(x...) = ArrayPartition((x...,))**

function ArrayPartition(x::S, ::Type{Val{copy_x}}=Val{false}) where {S<:Tuple,copy_x}
  T = promote_type(eltype.(x)...)
  if copy_x
    return ArrayPartition{T,S}(copy.(x))
  else
    return ArrayPartition{T,S}(x)
  end
end

```

My understanding of the bolded line is that it allowed me to have as an input more than one object of type tuple e.g. ArrayPartition((1,2), (3,4)). What I don’t understand however is what is the function of the colon after x… on the RHS, **ArrayPartition(x…) = ArrayPartition((x…,))**. More specifically, I know that (x…,) is actually ((1,2),(3,4)) but I can understand how this syntax ArrayPartition((x…,)) produces such result.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 29, 2018, 8:36pm UTC](https://discourse.julialang.org/t/trouble-in-understanding-how-to-use-varargs-as-a-constructor/14262/2 "2018-08-29T20:36:16Z")

</div>

`x...` is the [splatting operator](https://docs.julialang.org/en/v1/manual/faq/#The-two-uses-of-the-...-operator:-slurping-and-splatting-1). `(y,)` just produces a 1-Tuple. Combined, they create an N-tuple containing the elements of x:

```julia
julia> x = [(1,2),(3,4)]
2-element Array{Tuple{Int64,Int64},1}:
 (1, 2)
 (3, 4)

julia> (x...,)
((1, 2), (3, 4))

```

---

<div class="post-metadata">

**Author:** ![wsrinin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsrinin/32/7676_2.png) [@wsrinin](https://discourse.julialang.org/u/wsrinin)\
**Post date:** [August 29, 2018, 8:37pm UTC](https://discourse.julialang.org/t/trouble-in-understanding-how-to-use-varargs-as-a-constructor/14262/3 "2018-08-29T20:37:58Z")

</div>

Thank you, this is very clear. I was not aware of extra explanation of the splatting operation in the FAQ section.

---

<div class="post-metadata">

**Author:** ![wsrinin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsrinin/32/7676_2.png) [@wsrinin](https://discourse.julialang.org/u/wsrinin)\
**Post date:** [August 29, 2018, 10:27pm UTC](https://discourse.julialang.org/t/trouble-in-understanding-how-to-use-varargs-as-a-constructor/14262/4 "2018-08-29T22:27:15Z")

</div>

After playing around a bit more with the splatting operator, I found out that

```julia
(x...,) 

```

is actually the same as

```julia
tuple(x...)

```

My question now is in what sense " … " are used. Splitting or Splatting?

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [August 30, 2018, 2:59am UTC](https://discourse.julialang.org/t/trouble-in-understanding-how-to-use-varargs-as-a-constructor/14262/5 "2018-08-30T02:59:32Z")

</div>

`x...` used in a function type signature collects arguments into a tuple:

```julia
julia> f(args...) = typeof(args)
f (generic function with 1 method)

julia> f(1, 2, 3)
Tuple{Int64,Int64,Int64}

```

While `x...` in the context of a function call separates them out into individual args again. It does the opposite thing in opposite contexts.

---

<div class="post-metadata">

**Author:** ![wsrinin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsrinin/32/7676_2.png) [@wsrinin](https://discourse.julialang.org/u/wsrinin)\
**Post date:** [August 30, 2018, 4:28pm UTC](https://discourse.julialang.org/t/trouble-in-understanding-how-to-use-varargs-as-a-constructor/14262/6 "2018-08-30T16:28:25Z")

</div>

Thank you for the reply, but just to make sure that I understand what you are saying correctly. In the example above I have x = [(1,2), (3,4)]. In the case of Tuple(x…), x… is in the context of a function call. Hence, x… separates x into individual args which are (1,2) and (3,4). Thus Tuple(x…) is the same as Tuple((1,2),(3,4)). What is a function type signature (is it just typeof( ))?

In my OP, I also try to understand the meaning of

**ArrayPartition(x…) = ArrayPartition((x…,))**

My understanding now is that ArrayPartition(x…) is just a new constructor for ArrayPartition, and the meaning of x… in this case depends how the constructor is defined.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [August 30, 2018, 5:08pm UTC](https://discourse.julialang.org/t/trouble-in-understanding-how-to-use-varargs-as-a-constructor/14262/7 "2018-08-30T17:08:31Z")

</div>

```julia
ArrayPartition(x…) = ArrayPartition((x…,))

```

This is confusing because the splatting and tuple constructor in `ArrayPartition((x...,))` is redundant.

`ArrayPartition(x)` would do the same thing!

```julia
julia> x = (1,2,3)                                                                                                                             
(1, 2, 3)                                                                                                                                      
                                                                                                                                               
julia> y = (x...,)                                                                                                                             
(1, 2, 3)                                                                                                                                      

julia> x == y                                                                                                                                  
true                                                                                                                                           

```

So ArrayPartition is passed multiple arguments but it stores them all in a tuple in a single field.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [August 30, 2018, 5:15pm UTC](https://discourse.julialang.org/t/trouble-in-understanding-how-to-use-varargs-as-a-constructor/14262/8 "2018-08-30T17:15:51Z")

</div>

Also your understanding of splat in a function call is correct.

by “in a function type signature” I meant when used on a function argument like `f(x...) = x`  
sorry my descriptive terms still are a mess from Haskel and ruby and everything else, so my Julia has an accent…
