# Can I avoid field struct declaration?

**URL:** <https://discourse.julialang.org/t/can-i-avoid-field-struct-declaration/33671>\
**Category:** New to Julia\
**Created:** [January 22, 2020, 5:25pm UTC](https://discourse.julialang.org/t/can-i-avoid-field-struct-declaration/33671 "2020-01-22T17:25:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [January 22, 2020, 5:25pm UTC](https://discourse.julialang.org/t/can-i-avoid-field-struct-declaration/33671/1 "2020-01-22T17:25:18Z")

</div>

I don’t know how to search this topic, then I prefer to ask.  
If I did not misunderstood, the Constructor only works when all the field are supplied. I was wandering if I could define a struct and some of the fields will be generated by external functions (everything at once).  
A minimal example:

```julia
mutable struct MyType
    N::Integer
    S::Symbol
    r::Array{Float64,2}

    if S == :sy1
        r = foo(N)
    elseif S == :sy2
        r = foo2(N)
    end

    foo(N) = rand(N,2)
    foo2(N) = rand(N,3)
end

a = MyType(10, :sy1) # do not declare r
display(a.r)

```

(The code above does not work, but has the main idea)

---

<div class="post-metadata">

**Author:** ![Jakub\_Wronowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_wronowski/32/204030_2.png) [@Jakub\_Wronowski](https://discourse.julialang.org/u/Jakub_Wronowski)\
**Post date:** [January 22, 2020, 5:41pm UTC](https://discourse.julialang.org/t/can-i-avoid-field-struct-declaration/33671/2 "2020-01-22T17:41:54Z")

</div>

you need an inner constructor, or additional outer constructor if you want to leave option to initialize the r firld manually  
[https://docs.julialang.org/en/v1/manual/constructors/#Inner-Constructor-Methods-1](https://docs.julialang.org/en/v1/manual/constructors/#Inner-Constructor-Methods-1)

---

<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:** [January 22, 2020, 6:11pm UTC](https://discourse.julialang.org/t/can-i-avoid-field-struct-declaration/33671/3 "2020-01-22T18:11:19Z")

</div>

As always in Julia, you can just add methods to any function, also the constructor. It doesn’t have to be an inner constructor either:

```julia
mutable struct MyType
    N::Int
    S::Symbol
    r::Array{Float64,2}
end

function MyType(N, S)
    if S == :sy1
        return MyType(N, S, rand(N, 2))
    elseif S == :sy2
        return MyType(N, S, rand(N, 3))
    else
        error("Wrong input.")
    end
end

a = MyType(4, :sy2)

display(a.r)
4×3 Array{Float64,2}:
 0.310291 0.371418 0.751672  
 0.72183 0.932101 0.00485553
 0.58432 0.0196664 0.74358   
 0.762198 0.925545 0.776586

```

BTW, I changed the type of the `N` parameter from `Integer` to `Int`, since `Integer` is an abstract type, which normally leads to poor performance in structs. If you want to allow all sorts of integer types, you should make a parametric type.

---

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [January 22, 2020, 6:16pm UTC](https://discourse.julialang.org/t/can-i-avoid-field-struct-declaration/33671/4 "2020-01-22T18:16:07Z")

</div>

Thank you DNF.  
Indeed your code does what I want, but my wandering was if I could do `everything at once`, during the struct definition - I don’t know if this is a good idea, but I just want to know the possibility.

---

<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:** [January 22, 2020, 6:35pm UTC](https://discourse.julialang.org/t/can-i-avoid-field-struct-declaration/33671/5 "2020-01-22T18:35:53Z")

</div>

Does it really matter? Can’t you consider the constructor to be part of the definition?

For default values you can use a macro, from here [https://github.com/mauro3/Parameters.jl/blob/master/README.md](https://github.com/mauro3/Parameters.jl/blob/master/README.md)

Not sure if you can accomplish exactly what you want, though.

---

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [January 22, 2020, 6:45pm UTC](https://discourse.julialang.org/t/can-i-avoid-field-struct-declaration/33671/6 "2020-01-22T18:45:31Z")

</div>

Does not really matter 🙃  
As I sad, _just a curiosity_
