# How to assign tuples to struct?

**URL:** <https://discourse.julialang.org/t/how-to-assign-tuples-to-struct/49094>\
**Category:** General Usage\
**Created:** [October 27, 2020, 4:45am UTC](https://discourse.julialang.org/t/how-to-assign-tuples-to-struct/49094 "2020-10-27T04:45:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SonLe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sonle/32/9507_2.png) [@SonLe](https://discourse.julialang.org/u/SonLe)\
**Post date:** [October 27, 2020, 4:45am UTC](https://discourse.julialang.org/t/how-to-assign-tuples-to-struct/49094/1 "2020-10-27T04:45:23Z")

</div>

Hi everyone, I struggle with this issue.

Let say I have a tuple of 5 elements,

```julia
a = (100, 50, 0.05, 0.05, false, true)

```

and a struct is defined as,

```julia
struct PortfolioInfo
    num_stock::Int64
    num_rndreturn::Int64
    theta::Float64
    epsilon::Float64 
    norm1_constr::Bool
    norm2_constr::Bool
end

```

My question is, how could I assign the tuple to my struct?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 27, 2020, 4:51am UTC](https://discourse.julialang.org/t/how-to-assign-tuples-to-struct/49094/2 "2020-10-27T04:51:37Z")

</div>

`x=PortfolioInfo(a...)` should work. The `...` syntax is called splatting.

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [October 27, 2020, 5:48am UTC](https://discourse.julialang.org/t/how-to-assign-tuples-to-struct/49094/3 "2020-10-27T05:48:38Z")

</div>

@Oscar_Smith is correct, here’s an example, with an attempt at a non-splatted assignment just to show it doesn’t work.

```julia
julia> struct Foo a::Int
       b::Float64
       c::Bool
       end

julia> x=(1, 1.0, false)
(1, 1.0, false)

julia> Foo(x)
ERROR: MethodError: no method matching Foo(::Tuple{Int64,Float64,Bool})
Closest candidates are:
  Foo(::Any, ::Any, ::Any) at REPL[1]:1
  Foo(::Int64, ::Float64, ::Bool) at REPL[1]:1
Stacktrace:
 [1] top-level scope at REPL[2]:1

julia> Foo(x...)
Foo(1, 1.0, false)

```

---

<div class="post-metadata">

**Author:** ![SonLe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sonle/32/9507_2.png) [@SonLe](https://discourse.julialang.org/u/SonLe)\
**Post date:** [October 27, 2020, 6:27am UTC](https://discourse.julialang.org/t/how-to-assign-tuples-to-struct/49094/4 "2020-10-27T06:27:04Z")

</div>

It worked, I did not notice the splatting syntax. Thanks for your help @purplishrock @Oscar_Smith
