# Limited amount of fields in mutable struct with macros

**URL:** <https://discourse.julialang.org/t/limited-amount-of-fields-in-mutable-struct-with-macros/6061>\
**Category:** General Usage\
**Tags:** question, macros, type\
**Created:** [September 25, 2017, 1:09pm UTC](https://discourse.julialang.org/t/limited-amount-of-fields-in-mutable-struct-with-macros/6061 "2017-09-25T13:09:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)\
**Post date:** [September 25, 2017, 1:09pm UTC](https://discourse.julialang.org/t/limited-amount-of-fields-in-mutable-struct-with-macros/6061/1 "2017-09-25T13:09:19Z")

</div>

Hello,

First thanks for this community to be so active, it’s the first out of many problems I could not find a solution to!

I am trying to create mutable structs with a rather large amount of fields ( ~30 ). These structs are very similar so int order to avoid code repetition I created multiple macros and saved the fields in them.  
Now my problem is that my constructor does not find some of the fields.  
After investigation, the missing fields are the ones which replaced by “⋮” when “calling fieldnames(_name of struct_)”

I tried to make a similar case (many fields) without the macros, but there everything works…  
Any suggestion on this, I am using v0.6 by the way.

Thanks for you help. Théo

Code looks like

```julia
@def gaussianparametersfields begin
    μ::Array{Float64,1}
    η_1::Array{Float64,1}
    ζ::Array{Float64,2}
    η_2::Array{Float64,2}
end

mutable struct A
@commonfields
@gaussianparametersfields
@otherfields
*Some constructor*
end

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 25, 2017, 1:19pm UTC](https://discourse.julialang.org/t/limited-amount-of-fields-in-mutable-struct-with-macros/6061/2 "2017-09-25T13:19:34Z")

</div>

Please provide enough information for the code to be run. What is `@def`? What should I do to generate the error? Examples that can be copy pasted into the REPL are nice.

---

<div class="post-metadata">

**Author:** ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)\
**Post date:** [September 25, 2017, 1:46pm UTC](https://discourse.julialang.org/t/limited-amount-of-fields-in-mutable-struct-with-macros/6061/3 "2017-09-25T13:46:15Z")

</div>

@def is as defined in the documentation :

```julia
macro def(name, definition)
    return quote
        macro $(esc(name))()
            esc($(Expr(:quote, definition)))
        end
    end
end

```

My assumption on the error was wrong. It’s only a few fields that are not recognized. Here is the code that does not work

```julia
@def commonfields begin
    #Data
    X #Feature vectors
    y #Labels (-1,1)
    nSamples::Int64 # Number of data points
    nFeatures::Int64 # Number of features
    γ::Float64
    nEpochs::Int64
    µ_init::Array{Float64,1}
    VerboseLevel::Int64
    Stochastic::Bool
    Autotuning::Bool
    ρ_Θ::Array{Float64,1}
    κ_Θ::Float64
    τ_Θ::Int64
    AutotuningFrequency::Int64
    Trained::Bool
    TopMatrixForPrediction
    DownMatrixForPrediction
    MatricesPrecomputed::Bool
end

mutable struct A
@commonfields
end

in(:ρ_Θ, fieldnames(A))

```

Return false for ρ\_Θ, κ\_Θ and τ\_Θ  
Thanks for your quick reply

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 25, 2017, 1:50pm UTC](https://discourse.julialang.org/t/limited-amount-of-fields-in-mutable-struct-with-macros/6061/4 "2017-09-25T13:50:45Z")

</div>

> [@theogf](#):
>
> @def is as defined in the documentation :

What documentation? It is not a Base macro.

Anyway, I get:

```julia
julia> in(:ρ_Θ, fieldnames(A))
true

julia> in(:κ_Θ, fieldnames(A))
true

julia> in(:τ_Θ, fieldnames(A))
true

```

On julia v0.6.

---

<div class="post-metadata">

**Author:** ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)\
**Post date:** [September 25, 2017, 2:41pm UTC](https://discourse.julialang.org/t/limited-amount-of-fields-in-mutable-struct-with-macros/6061/5 "2017-09-25T14:41:38Z")

</div>

> What documentation? It is not a Base macro.

It is indeed not in the Base macro, I misexplained myself, it is simply that I have seen many times suggested and thought it was somehow an “official” method.  
Anyway, running this exact code still gives me a false statement.  
But I just found the cause and this is very vicious:  
ρ\_Θ and `ρ_Θ` are not the same according to Julia. Although I wrote them everytime with \theta ![error on theta](https://global.discourse-cdn.com/julialang/original/3X/a/e/aeb0166516d0fecac90878cb92b66039f8b223d0.jpeg)

(I am running Julia from Ubuntu btw)  
Well thanks for your help, I will just change the names to something different

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [September 25, 2017, 2:43pm UTC](https://discourse.julialang.org/t/limited-amount-of-fields-in-mutable-struct-with-macros/6061/6 "2017-09-25T14:43:04Z")

</div>

> [@theogf](#):
>
> @def gaussianparametersfields begin  
> μ::Array{Float64,1}  
> η\_1::Array{Float64,1}

One Greek symbol is uppercase, and one is lowercase?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 25, 2017, 2:46pm UTC](https://discourse.julialang.org/t/limited-amount-of-fields-in-mutable-struct-with-macros/6061/7 "2017-09-25T14:46:04Z")

</div>

> [@theogf](#):
>
> ρ\_Θ and ρ\_Θ

The thetas there seems to be the same to me? If you enter them in the REPL as a char, e.g:

```julia
julia> 'Θ'
'Θ': Unicode U+0398 (category Lu: Letter, uppercase)

```

what does it say for the two thetas?

---

<div class="post-metadata">

**Author:** ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)\
**Post date:** [September 25, 2017, 2:50pm UTC](https://discourse.julialang.org/t/limited-amount-of-fields-in-mutable-struct-with-macros/6061/8 "2017-09-25T14:50:12Z")

</div>

From \theta :

```julia
'θ': Unicode U+03b8 (category Ll: Letter, lowercase)

```

From fieldnames

```julia
'Θ': Unicode U+0398 (category Lu: Letter, uppercase)

```

My guess is that the macro interpretated the lowercase θ as an uppercase Θ while in the rest of the code the lowercase is used (without passing by a macro)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 25, 2017, 3:50pm UTC](https://discourse.julialang.org/t/limited-amount-of-fields-in-mutable-struct-with-macros/6061/10 "2017-09-25T15:50:24Z")

</div>

Ah, like Petr says, this is just capital theta vs lowercase theta.
