# Creating an immutable struct with new()

**URL:** <https://discourse.julialang.org/t/creating-an-immutable-struct-with-new/34583>\
**Category:** New to Julia\
**Created:** [February 13, 2020, 3:08pm UTC](https://discourse.julialang.org/t/creating-an-immutable-struct-with-new/34583 "2020-02-13T15:08:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bocc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bocc/32/7546_2.png) [@bocc](https://discourse.julialang.org/u/bocc)\
**Post date:** [February 13, 2020, 3:08pm UTC](https://discourse.julialang.org/t/creating-an-immutable-struct-with-new/34583/1 "2020-02-13T15:08:57Z")

</div>

Hello there,

I wanted to create a struct with the `new()` construct, mostly for convenience reasons, I wanted to identify it’s fields by their names (as it will have a lot of fields but will be read-only after initialization). So I wrote something along the lines of:

```julia
struct Tytto
    f1

    function Tytto()
        t = new()
        t.f1 = 1
        
        return t
    end
end

```

But Julia reports that I cannot call `setfield()` on an immutable struct when executing `Tytto()`. So my questions is, is there a way to initialize a struct by referring to field names in the constructor?

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [February 13, 2020, 3:13pm UTC](https://discourse.julialang.org/t/creating-an-immutable-struct-with-new/34583/2 "2020-02-13T15:13:06Z")

</div>

I’m not aware of anything…what I tend to do in this situation is:

```julia
struct Tytto
    f1
    f2
    f3
    f4
    function Tytto()
       local f1 = 1
       local f2 = some_func(f1)
       local f3 = another_func(f2, f1)
       local f4 = yya()
       Tytto(f1, f2, f3, f4)
    end
end

```

Not as fool proof as being able to assign the fields directly, but keeps thing readable.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 13, 2020, 3:21pm UTC](https://discourse.julialang.org/t/creating-an-immutable-struct-with-new/34583/3 "2020-02-13T15:21:42Z")

</div>

You can use `Base.@kwdef` to make a constructor that takes keyword arguments:

```julia
julia> using Base: @kwdef

julia> @kwdef struct Tytto
         f1
         f2
       end
Tytto

julia> Tytto(f1 = 1, f2 = 2)
Tytto(1, 2)

```

It’s not exactly what you’re asking for, but it might accomplish the same general task.

---

<div class="post-metadata">

**Author:** ![bocc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bocc/32/7546_2.png) [@bocc](https://discourse.julialang.org/u/bocc)\
**Post date:** [February 13, 2020, 4:59pm UTC](https://discourse.julialang.org/t/creating-an-immutable-struct-with-new/34583/4 "2020-02-13T16:59:34Z")

</div>

Thank you, those are nice workarounds!
