# Structure Constructors

**URL:** <https://discourse.julialang.org/t/structure-constructors/23822>\
**Category:** New to Julia\
**Created:** [May 3, 2019, 5:11pm UTC](https://discourse.julialang.org/t/structure-constructors/23822 "2019-05-03T17:11:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thaerious](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thaerious/32/8289_2.png) [@Thaerious](https://discourse.julialang.org/u/Thaerious)\
**Post date:** [May 3, 2019, 5:11pm UTC](https://discourse.julialang.org/t/structure-constructors/23822/1 "2019-05-03T17:11:59Z")

</div>

I have a simple structure with an array and an associate (fitness) value.

```julia
    mutable struct Solution
        genome::Array
        fitness
    end

```

I want to put in a zero’d constructor so I did the following and got “I get this error LoadError: MethodError: no method matching Main.Scratch.Solution{Int64}(::Int64)”.

```julia
mutable struct Solution{T}
        genome::Vector{T}
        fitness
        Solution(vector::Vector, fitness) = new{T}(vector, fitness)
        Solution(sz::Int64) = new{T}(zeros(T, sz), 0)
end

```

if I do the following I get ‘LoadError: UndefVarError: T not defined’;

```julia
mutable struct Solution{T}
        genome::Vector{T}
        fitness
        Solution{T}(vector::Vector, fitness) = new{T}(vector, fitness)
        Solution{T}(sz::Int64) = new{T}(zeros(T, sz), 0)
    end

```

I’m at a loss how to form these constructors properly.

---

<div class="post-metadata">

**Author:** ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)\
**Post date:** [May 3, 2019, 6:01pm UTC](https://discourse.julialang.org/t/structure-constructors/23822/2 "2019-05-03T18:01:48Z")

</div>

The first constructor needs a `where` clause:

```julia
Solution(genome::Vector{T}, fitness) where T = new{T}(genome, fitness)

```

In the second constructor you need to provide the type information

```julia
Solution(::Type{T}, sz::Int) where T = new{T}(zeros(T, sz), 0)

```

Then you can call it like this (`Wax` is the name of my test module)

```julia
julia> Wax.Solution(Int, 7)
Main.Wax.Solution{Int64}([0, 0, 0, 0, 0, 0, 0], 0)

```

It also possible to provide a constructor with a default type (`Float64`):

```julia
mutable struct Solution{T}
    genome::Vector{T}
    fitness
    Solution(genome::Vector{T}, fitness) where T = new{T}(genome, fitness)
    Solution(::Type{T}, sz::Int) where T = new{T}(zeros(T, sz), 0)
    Solution(sz::Int) = Solution(Float64, sz)
end

```

---

<div class="post-metadata">

**Author:** ![Thaerious](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thaerious/32/8289_2.png) [@Thaerious](https://discourse.julialang.org/u/Thaerious)\
**Post date:** [May 3, 2019, 9:54pm UTC](https://discourse.julialang.org/t/structure-constructors/23822/3 "2019-05-03T21:54:05Z")

</div>

Thanks! I’ll give it a try.
