# Initialize fields of user-defined \`types\` in arbitrary order

**URL:** https://discourse.julialang.org/t/initialize-fields-of-user-defined-types-in-arbitrary-order/8109
**Category:** New to Julia
**Tags:** type
**Created:** [January 2, 2018, 12:47pm UTC](https://discourse.julialang.org/t/initialize-fields-of-user-defined-types-in-arbitrary-order/8109 "2018-01-02T12:47:54Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![SepandMeenu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sepandmeenu/32/8846_2.png) [@SepandMeenu](https://discourse.julialang.org/u/SepandMeenu)
#### Post date: [January 2, 2018, 12:47pm UTC](https://discourse.julialang.org/t/initialize-fields-of-user-defined-types-in-arbitrary-order/8109/1 "2018-01-02T12:47:54Z")

</div>

I’d like to know if it is possible to initialize the fields of a user-defined `type` (or `struct`) in a deliberate order by using the field names so that the order of defined variables in the code does not matter. In other words, my question is about using keywords in an `inner` or `outer` `constructor`.

I have something like the following snippet in mind, where the order of initialization of fields is opposite to the order of their appearance in the code:

```julia
    # Julia ver. 0.4.7

    type MyType
        x1
        x2
    
        MyType(y) = new(x2 = y, x1 = 0) # inner constructor
    end

    MyType(a, b) = new(x2 = a, x1 = b) # outer constructor

```

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [January 2, 2018, 12:48pm UTC](https://discourse.julialang.org/t/initialize-fields-of-user-defined-types-in-arbitrary-order/8109/2 "2018-01-02T12:48:19Z")

</div>

Check out the [Parameters.jl](https://github.com/mauro3/Parameters.jl) package to do this.

---

<div class="post-metadata">

### Author: ![bicycle1885](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bicycle1885/32/107_2.png) [@bicycle1885](https://discourse.julialang.org/u/bicycle1885)
#### Post date: [January 2, 2018, 1:36pm UTC](https://discourse.julialang.org/t/initialize-fields-of-user-defined-types-in-arbitrary-order/8109/3 "2018-01-02T13:36:57Z")

</div>

`Base.@kwdef` macro is available out of the box (at least on Julia 0.6):

```julia
julia> Base.@kwdef struct MyType
           x::Int
           y::String = "default"
       end
MyType

julia> MyType(x=10)
MyType(10, "default")

julia> MyType(x=10, y="junk")
MyType(10, "junk")

julia> MyType(y="junk", x=10)
MyType(10, "junk")

```

---

<div class="post-metadata">

### Author: ![SepandMeenu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sepandmeenu/32/8846_2.png) [@SepandMeenu](https://discourse.julialang.org/u/SepandMeenu)
#### Post date: [January 2, 2018, 2:00pm UTC](https://discourse.julialang.org/t/initialize-fields-of-user-defined-types-in-arbitrary-order/8109/4 "2018-01-02T14:00:15Z")

</div>

isn’t that clunky to need a macro for such a simple task? 🙄

---

<div class="post-metadata">

### Author: ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)
#### Post date: [January 2, 2018, 2:04pm UTC](https://discourse.julialang.org/t/initialize-fields-of-user-defined-types-in-arbitrary-order/8109/5 "2018-01-02T14:04:58Z")

</div>

Macros are a first-class, core language feature and are excellent and widely-used for implementing these types of annotations (`@inline @simd` etc.).

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [January 2, 2018, 2:11pm UTC](https://discourse.julialang.org/t/initialize-fields-of-user-defined-types-in-arbitrary-order/8109/6 "2018-01-02T14:11:04Z")

</div>

That issue has come up for us, because we have some types where, for reasons of space, we put the fields in the best order for packing with as little padding as possible, or to match some C structure, which doesn’t match what seems natural as far as a constructor.

---

<div class="post-metadata">

### Author: ![SepandMeenu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sepandmeenu/32/8846_2.png) [@SepandMeenu](https://discourse.julialang.org/u/SepandMeenu)
#### Post date: [January 2, 2018, 2:16pm UTC](https://discourse.julialang.org/t/initialize-fields-of-user-defined-types-in-arbitrary-order/8109/7 "2018-01-02T14:16:27Z")

</div>

are there any implications for performance in case one uses the above method with `Base.@kwdef` ?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [January 2, 2018, 2:37pm UTC](https://discourse.julialang.org/t/initialize-fields-of-user-defined-types-in-arbitrary-order/8109/8 "2018-01-02T14:37:17Z")

</div>

Cross posted: [constructor - Initialize fields of user-defined `types` in arbitrary order - Stack Overflow](https://stackoverflow.com/questions/48060390/initialize-fields-of-user-defined-types-in-arbitrary-order)

---

<div class="post-metadata">

### Author: ![SepandMeenu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sepandmeenu/32/8846_2.png) [@SepandMeenu](https://discourse.julialang.org/u/SepandMeenu)
#### Post date: [January 2, 2018, 2:48pm UTC](https://discourse.julialang.org/t/initialize-fields-of-user-defined-types-in-arbitrary-order/8109/9 "2018-01-02T14:48:04Z")

</div>

that’s my own question.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [January 2, 2018, 2:51pm UTC](https://discourse.julialang.org/t/initialize-fields-of-user-defined-types-in-arbitrary-order/8109/10 "2018-01-02T14:51:31Z")

</div>

> [@SepandMeenu](#):
>
> that’s my own question.

That’s the point. I am letting other people know that you asked the same question on other sites so that way we can make sure people who are looking to help you can see what’s already been answered instead of just a small snippet from this site. I am sure someone would have added a snippet on keyword arguments and splatting, but it’s already laid out at that other site and would thus be a waste of their time. This is the reason why it’s generally recommended (on all forums) to not cross-post. Please take this as a (very gentle) warning: it’s not something that should be done but it’s not really too bad.

---

<div class="post-metadata">

### Author: ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)
#### Post date: [January 2, 2018, 3:14pm UTC](https://discourse.julialang.org/t/initialize-fields-of-user-defined-types-in-arbitrary-order/8109/11 "2018-01-02T15:14:14Z")

</div>

> [@ChrisRackauckas](#):
>
> it’s not something that should be done but it’s really too bad.

Too bad in comparison to what? I’m confused now.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [January 2, 2018, 3:16pm UTC](https://discourse.julialang.org/t/initialize-fields-of-user-defined-types-in-arbitrary-order/8109/12 "2018-01-02T15:16:25Z")

</div>

> [@lobingera](#):
>
> Too bad in comparison to what? I’m confused now.

Typo. It’s not really too bad.
