# Creating a struct containing "derived variables"

**URL:** <https://discourse.julialang.org/t/creating-a-struct-containing-derived-variables/80841>\
**Category:** New to Julia\
**Tags:** question, struct\
**Created:** [May 10, 2022, 7:49pm UTC](https://discourse.julialang.org/t/creating-a-struct-containing-derived-variables/80841 "2022-05-10T19:49:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![james3](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@james3](https://discourse.julialang.org/u/james3)\
**Post date:** [May 10, 2022, 7:49pm UTC](https://discourse.julialang.org/t/creating-a-struct-containing-derived-variables/80841/1 "2022-05-10T19:49:36Z")

</div>

I am using a `struct` to hold parameters of a model. For example, let’s say I have two parameters `sigma` and `mu`. It is convenient to also store some variables that are functions of these parameters in the same struct. For example, I may create a `grid` that can be calculated as a function of `sigma` and `mu`. So, I have the `struct` storing

```julia
struct mytype
    sigma::Float64
    mu::Float64
    grid::Array{Float64}
end

```

What I want to do, is to create an object of type `mytype`, giving it values for `sigma` and `alpha`, with the element `grid` then created automatically by a function that I would define. Is this possible?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 10, 2022, 7:53pm UTC](https://discourse.julialang.org/t/creating-a-struct-containing-derived-variables/80841/2 "2022-05-10T19:53:43Z")

</div>

Sure, you only need to define a constructor:

```julia
julia> struct A
           sigma::Float64
           computed::Vector{Float64}
       end

julia> function A(sigma)
           computed = [sigma*i for i in 1:10]
           return A(sigma, computed)
       end
A

julia> A(0.1)
A(0.1, [0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6000000000000001, 0.7000000000000001, 0.8, 0.9, 1.0])

```

(note that `Array{Float64}` is an abstract type, you probably want `Array{Float64,1}`, which is the same as `Vector{Float64}`)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 10, 2022, 7:56pm UTC](https://discourse.julialang.org/t/creating-a-struct-containing-derived-variables/80841/3 "2022-05-10T19:56:30Z")

</div>

> [@lmiq](#):
>
> you only need to define a constructor

(Or define any function that returns your `struct`, for that matter. A constructor is just a conveniently named function for a “standard” way to form your `struct`, especially if you need to enforce some requirement that _must_ be true of your `struct` fields, but if there are multiple different ways you might want produce your `struct` then you might want multiple different function names.)

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 10, 2022, 8:31pm UTC](https://discourse.julialang.org/t/creating-a-struct-containing-derived-variables/80841/4 "2022-05-10T20:31:08Z")

</div>

You may also want to take a look at the not-so-well-known [`Base.@kwdef`](https://discourse.julialang.org/t/what-does-kwdef-do/51973) macro
