# Custom structure constructor

**URL:** <https://discourse.julialang.org/t/custom-structure-constructor/88958>\
**Category:** General Usage\
**Created:** [October 19, 2022, 12:09pm UTC](https://discourse.julialang.org/t/custom-structure-constructor/88958 "2022-10-19T12:09:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maucejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maucejo/32/39090_2.png) [@Maucejo](https://discourse.julialang.org/u/Maucejo)\
**Post date:** [October 19, 2022, 12:09pm UTC](https://discourse.julialang.org/t/custom-structure-constructor/88958/1 "2022-10-19T12:09:14Z")

</div>

Dear all,

I have a question about the use of custom constructor to initialize a Julia struct. I would like to initialize a parameter z (operational parameter) of a structure from some physical parameters, e.g. x and y. To clarify the problem, let’s consider the following code snippet

```julia
# ERROR
struct Foo
    z

    function Foo(x, y)
        self = new()
        self.z = x + y
       return self
    end
end

```

This code raises an error. To make it work, I need to declare the structure Foo as mutable, which is not desired proper, since z doesn’t mutate.

```julia
mutable struct Foo
    z

    function Foo(x, y)
        self = new()
        self.z = x + y
       return self
    end
end

```

What is the Julian way to do this (i.e. keep the structure immutable, while initializing it from external parameters) ?

Thank you for your answers.

---

<div class="post-metadata">

**Author:** ![magister-ludi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magister-ludi/32/4003_2.png) [@magister-ludi](https://discourse.julialang.org/u/magister-ludi)\
**Post date:** [October 19, 2022, 12:18pm UTC](https://discourse.julialang.org/t/custom-structure-constructor/88958/2 "2022-10-19T12:18:54Z")

</div>

This is the usual way to do something like that:

```julia
struct Foo
    z
    function Foo(x, y)
        self = new(x + y)
       return self
    end
end

```

I add the usual advice that it’s better in the long run to declare the `struct` so that `z` has a concrete or a parameterized type.

---

<div class="post-metadata">

**Author:** ![Maucejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maucejo/32/39090_2.png) [@Maucejo](https://discourse.julialang.org/u/Maucejo)\
**Post date:** [October 19, 2022, 3:17pm UTC](https://discourse.julialang.org/t/custom-structure-constructor/88958/3 "2022-10-19T15:17:37Z")

</div>

Thank you for the answer. I will follow your advice 😀

---

<div class="post-metadata">

**Author:** ![magister-ludi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magister-ludi/32/4003_2.png) [@magister-ludi](https://discourse.julialang.org/u/magister-ludi)\
**Post date:** [October 20, 2022, 12:34am UTC](https://discourse.julialang.org/t/custom-structure-constructor/88958/4 "2022-10-20T00:34:55Z")

</div>

I should have pointed you to the [documentation](https://docs.julialang.org/en/v1/manual/constructors/) about constructors. It’s probably worth reading. If there’s something you don’t understand, come back and ask.
