# Kwargs in new(), or safer ways of constructing immutable structs

**URL:** <https://discourse.julialang.org/t/kwargs-in-new-or-safer-ways-of-constructing-immutable-structs/43555>\
**Category:** New to Julia\
**Created:** [July 23, 2020, 1:59pm UTC](https://discourse.julialang.org/t/kwargs-in-new-or-safer-ways-of-constructing-immutable-structs/43555 "2020-07-23T13:59:12Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![Benjia](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@Benjia](https://discourse.julialang.org/u/Benjia)\
**Post date:** [July 23, 2020, 1:59pm UTC](https://discourse.julialang.org/t/kwargs-in-new-or-safer-ways-of-constructing-immutable-structs/43555/1 "2020-07-23T13:59:12Z")

</div>

When creating an immutable struct with a reasonable number of fields and an inner constructor, I find that aligning the arguments to `new()` with the order of the struct fields to be quite tedious and error prone (especially if adjacent fields have the same type, or I add / reorder types). With a mutable struct, I tend to do the following:

```julia
ret = new()
ret.x = x
ret.y = y
ret.z = z
...
return ret

```

Which is much safer and easier to maintain.

Does anyone have a nice approach to this? It seems like `new()` doesn’t support kwargs, but it would be cool if it did. I wonder if a macro could be written that would allow `new()` to take kwargs?

---

_[View the full topic](https://discourse.julialang.org/t/kwargs-in-new-or-safer-ways-of-constructing-immutable-structs/43555)._
