# How can I push data to a Type when the data has more fields than I want

**URL:** <https://discourse.julialang.org/t/how-can-i-push-data-to-a-type-when-the-data-has-more-fields-than-i-want/15657>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 29, 2018, 8:57am UTC](https://discourse.julialang.org/t/how-can-i-push-data-to-a-type-when-the-data-has-more-fields-than-i-want/15657 "2018-09-29T08:57:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 29, 2018, 8:57am UTC](https://discourse.julialang.org/t/how-can-i-push-data-to-a-type-when-the-data-has-more-fields-than-i-want/15657/1 "2018-09-29T08:57:19Z")

</div>

I got this code:

```julia
struct Test
  rest::String
end

function returnTest(args...)
  return Test(args...)
end

returnTest("This will be the rest string", "excess data", "some more uneeded")
# How will it return Test("This will be the rest string") without the error of it trying to push data to fields that aren't there

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 29, 2018, 9:09am UTC](https://discourse.julialang.org/t/how-can-i-push-data-to-a-type-when-the-data-has-more-fields-than-i-want/15657/2 "2018-09-29T09:09:31Z")

</div>

I’m a bit confused by your question, it this what you want:

```julia
function returnTest(args...) 
  return Test(args[1])
end

```

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 29, 2018, 9:12am UTC](https://discourse.julialang.org/t/how-can-i-push-data-to-a-type-when-the-data-has-more-fields-than-i-want/15657/3 "2018-09-29T09:12:41Z")

</div>

I want my code to be automated, it will work with way more data and there will be excessive fields there  
So I want only the data that the type will accept to get pushed without an error.  
the whole point of it is to not do

```julia
function returnTest(args...)
  return Test(args[1], args[2], args[3])
end

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [September 29, 2018, 10:16am UTC](https://discourse.julialang.org/t/how-can-i-push-data-to-a-type-when-the-data-has-more-fields-than-i-want/15657/4 "2018-09-29T10:16:12Z")

</div>

One way:

```julia
julia> struct MyTest
           a::Int
           b::Int
       end

julia> getargs(T) = [:(args[$i]) for i in 1:fieldcount(T)]
getargs (generic function with 1 method)

julia> @eval MyTest(args...) = MyTest($(getargs(MyTest)...))
MyTest

julia> MyTest(1, 2, 3, 4)
MyTest(1, 2)

julia> @code_warntype MyTest(1, 2, 3, 4)
Body::MyTest
1 1 ─ %1 = (Base.getfield)(args, 1, true)::Int64 │╻ getindex
  │ %2 = (Base.getfield)(args, 2, true)::Int64 ││
  │ %3 = %new(Main.MyTest, %1, %2)::MyTest │╻ Type
  └── return %3                                   

```

Probably an over-kill though, you can just use `(args[1], args[2])` instead for your type, but the solution above can be generalized to multiple types, each with its own `fieldcount` as such:

```julia
julia> struct MyTest
           a::Int
           b::Int
       end

julia> struct MyTest2
           a::Int
           b::Int
           c::Int
       end

julia> getargs(T) = [:(args[$i]) for i in 1:fieldcount(T)]
getargs (generic function with 1 method)

julia> for T in (:MyTest, :MyTest2)
           @eval $T(args...) = $T($(getargs(eval(T))...))
       end

julia> MyTest(1, 2, 3, 4)
MyTest(1, 2)

julia> MyTest2(1, 2, 3, 4)
MyTest2(1, 2, 3)

```

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 29, 2018, 10:47am UTC](https://discourse.julialang.org/t/how-can-i-push-data-to-a-type-when-the-data-has-more-fields-than-i-want/15657/5 "2018-09-29T10:47:46Z")

</div>

Hey, thank you for your time I managed to write this and it’s actually getting the work done

```julia
    function construct(userData)
        wantedFields = fieldnames(Self)
        data = []

        for i in wantedFields
            field = i |> String
            push!(data, userData[field])
        end
        return Self(data...)
    end

```
