# Best Way to Initialize this Struct

**URL:** <https://discourse.julialang.org/t/best-way-to-initialize-this-struct/70313>\
**Category:** General Usage\
**Tags:** struct\
**Created:** [October 25, 2021, 2:30am UTC](https://discourse.julialang.org/t/best-way-to-initialize-this-struct/70313 "2021-10-25T02:30:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [October 25, 2021, 2:30am UTC](https://discourse.julialang.org/t/best-way-to-initialize-this-struct/70313/1 "2021-10-25T02:30:09Z")

</div>

I would appreciate any opinions or thoughts on the following. I want to create a struct and I want one of the fields of the struct to be dependent on the others. Below is how I’ve accomplished this as a first pass.

```julia
struct MyStruct
    a::Int
    b::Real
    grid::Matrix{Float64}
end

function MyStruct(a::Int, b::Real)::MyStruct
    grid = zeros(Float64, a, a,)
    grid[1, 2:end-1] .= b
    grid[2:end-1, end] .= b
    return MyStruct(a, b, grid)
end

```

I’m wondering if this implementation is proper or if there is a better or more “julia” way of implementing it.

Edit: I should have made this clearer. I want the `grid` to be determined from `a` and `b`.

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [October 25, 2021, 2:53am UTC](https://discourse.julialang.org/t/best-way-to-initialize-this-struct/70313/2 "2021-10-25T02:53:00Z")

</div>

`a` seems to be redundant since you can infer it from the size of grid? But not a big deal.

However, I would define the struct as

```julia
struct MyStruct{T} 
    a::Int
    b::T
    grid::Matrix{T}
end

```

Or

```julia
struct MyStruct{B, T} 
    a::Int
    b::B
    grid::Matrix{T}
end

```

Since `Real` is not a concrete type and can make the struct slow if you pass it around often. Now the struct size is completely clear from the type signature and the derived types in the body.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [October 25, 2021, 12:39pm UTC](https://discourse.julialang.org/t/best-way-to-initialize-this-struct/70313/3 "2021-10-25T12:39:24Z")

</div>

To clarify: Do you want `grid` to take the values from `b` at initialization only or always?

---

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [October 25, 2021, 11:39pm UTC](https://discourse.julialang.org/t/best-way-to-initialize-this-struct/70313/4 "2021-10-25T23:39:03Z")

</div>

At initialization only.

---

<div class="post-metadata">

**Author:** ![Egwene\_al\_Vere](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@Egwene\_al\_Vere](https://discourse.julialang.org/u/Egwene_al_Vere)\
**Post date:** [October 26, 2021, 12:13am UTC](https://discourse.julialang.org/t/best-way-to-initialize-this-struct/70313/5 "2021-10-26T00:13:27Z")

</div>

You may also be interested in `Parameters.jl` [here](https://github.com/mauro3/Parameters.jl), which enables keyword-based initialization as well as fields that depend on other fields (search for “Parameter interdependence” from the doc [here](https://mauro3.github.io/Parameters.jl/stable/manual/))

ex.

```julia
julia> using Parameters

julia> @with_kw struct foo
        a::Int
        b::Real
        grid::Matrix{Float64} = g(a,b)
        end
foo

julia> function g(a,b)
           grid = zeros(Float64, a, a,)
           grid[1, 2:end-1] .= b
           grid[2:end-1, end] .= b
           grid
       end
g (generic function with 1 method)

julia> x = foo(a=4,b=1.2)
foo
  a: Int64 4
  b: Float64 1.2
  grid: Array{Float64}((4, 4)) [0.0 1.2 1.2 0.0; 0.0 0.0 0.0 1.2; 0.0 0.0 0.0 1.2; 0.0 0.0 0.0 0.0]

julia> x.grid
4×4 Matrix{Float64}:
 0.0 1.2 1.2 0.0
 0.0 0.0 0.0 1.2
 0.0 0.0 0.0 1.2
 0.0 0.0 0.0 0.0

```
