# Julian way of writing a constructor

**URL:** https://discourse.julialang.org/t/julian-way-of-writing-a-constructor/60943
**Category:** General Usage
**Tags:** question
**Created:** [May 11, 2021, 11:50am UTC](https://discourse.julialang.org/t/julian-way-of-writing-a-constructor/60943 "2021-05-11T11:50:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [May 11, 2021, 11:50am UTC](https://discourse.julialang.org/t/julian-way-of-writing-a-constructor/60943/1 "2021-05-11T11:50:15Z")

</div>

I have the following code:

```julia
using StaticArrays

# Type definitions
const MyFloat = Float32
const Vec3 = MVector{3, MyFloat}

mutable struct State
    v_wind::Vec3 # wind vector at the height of the kite
    v_wind_gnd::Vec3 # wind vector at reference height
    v_wind_tether::Vec3
    v_apparent::Vec3
    drag_force::Vec3
    lift_force::Vec3
    steering_force::Vec3
    last_force::Vec3
    spring_force::Vec3
    kite_y::Vec3
    segment::Vec3
    seg_area::MyFloat # area of one tether segment
    c_spring::MyFloat
    length::MyFloat
    damping::MyFloat
    area::MyFloat
    param_cl::MyFloat
    param_cd::MyFloat
    v_app_norm::MyFloat
    cor_steering::MyFloat
    psi::MyFloat
    beta::MyFloat
end

function init()
    state = State(zeros(3), zeros(3), zeros(3), zeros(3), zeros(3), zeros(3), zeros(3), zeros(3), zeros(3), zeros(3), zeros(3), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
    state.v_wind[1] = 8 # westwind, downwind direction to the east
    state.v_wind_gnd[1] = 8 # westwind, downwind direction to the east
    state.v_wind_tether[1] = 8 # wind at half of the height of the kite
    state.v_apparent = [1.0, 2.0, 3.0]
    state.param_cl = 0.2
    state.param_cd = 1.0
    state
end
const state = init()

```

How can this be written in a more Julian way?

Is there a way to initialize the struct with zeros without having to write all fields by hand?

Is there a better way to write a constructor?

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [May 11, 2021, 12:10pm UTC](https://discourse.julialang.org/t/julian-way-of-writing-a-constructor/60943/2 "2021-05-11T12:10:39Z")

</div>

Maybe something like this:

```julia
using Parameters

@with_kw mutable struct State{S, T}
    v_wind::T = zero(T) # wind vector at the height of the kite
    v_wind_gnd::T = zero(T) # wind vector at reference height
    v_wind_tether::T = zero(T)
    v_apparent::T = zero(T)
    drag_force::T = zero(T)
    lift_force::T = zero(T)
    steering_force::T = zero(T)
    last_force::T = zero(T)
    spring_force::T = zero(T)
    kite_y::T = zero(T)
    segment::T = zero(T)
    seg_area::S = zero(S) # area of one tether segment
    c_spring::S = zero(S)
    length::S = zero(S)
    damping::S = zero(S)
    area::S = zero(S)
    param_cl::S = zero(S)
    param_cd::S = zero(S)
    v_app_norm::S = zero(S)
    cor_steering::S = zero(S)
    psi::S = zero(S)
    beta::S = zero(S)
end

s = State{Float32, MVector{3,Float32}}()

```

I would parameterize the struct instead of forcing a specific precision and vector type.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [May 11, 2021, 1:42pm UTC](https://discourse.julialang.org/t/julian-way-of-writing-a-constructor/60943/3 "2021-05-11T13:42:30Z")

</div>

Yes, this is nice! 🙂

Thank you!

---

<div class="post-metadata">

### Author: ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)
#### Post date: [May 11, 2021, 1:57pm UTC](https://discourse.julialang.org/t/julian-way-of-writing-a-constructor/60943/4 "2021-05-11T13:57:55Z")

</div>

Instead of Parameters.jl, you can also use `Base.@kwdef`.

---

<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 11, 2021, 2:18pm UTC](https://discourse.julialang.org/t/julian-way-of-writing-a-constructor/60943/5 "2021-05-11T14:18:12Z")

</div>

As a side note, your `MVectors` are still mutable even if the `State` as a whole is not mutable. Thus, if the scalars do not have to be mutable (or if only a a few of them have), you may better choose to use an immutable struct.

(mutating a scalar in an overall immutable struct requires some strategy of the ones described [here](https://discourse.julialang.org/t/mutable-scalar-in-immutable-object-the-best-alternative/57401))

Also, `Parameters` offers the `@unpack` macro, which can be used with, for example:

```julia
@unpack v_wind, area = s

```

where `s` is of type `State`, in a function that you might be using only these two variables among all those contained in the struct.
