# Parametric Field Names

**URL:** <https://discourse.julialang.org/t/parametric-field-names/56428>\
**Category:** General Usage\
**Created:** [March 3, 2021, 6:03pm UTC](https://discourse.julialang.org/t/parametric-field-names/56428 "2021-03-03T18:03:27Z")\
**Posts on this page:** 1\
**Showing post:** 15

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [March 3, 2021, 8:18pm UTC](https://discourse.julialang.org/t/parametric-field-names/56428/15 "2021-03-03T20:18:24Z")

</div>

Personally I’am not a huge friend of storing redundant data, and you can avoid any illegal instance by just storing the essential information once. Found this originally [here](https://discourse.julialang.org/t/getproperty-decorations-inheritance-in-0-7/11237/3) and meanwhile using this quite extensively:

```julia
mutable struct Square
    side
end

# Acutally not strictly needed just for completeness
# see https://docs.julialang.org/en/v1/base/base/#Base.propertynames   
propertynames(p::Square,private=true) = begin
    fields = fieldnames(typeof(p))    
    (fields...,:area,:diagonal)
end

#Dispatch the gets
Base.getproperty(p::Square,n::Symbol) = getproperty(p::Square,Val{n}())
Base.getproperty(p::Square,::Val{S}) where {S} = getfield(p,S) #generic fallback
Base.getproperty(p::Square,::Val{:area}) = p.side^2
Base.getproperty(p::Square,::Val{:diagonal}) = √2*p.side

#Dispatch the sets
Base.setproperty!(p::Square,n::Symbol,x) = setproperty!(p,Val{n}(),x)
Base.setproperty!(p::Square,::Val{S},x) where {S} = setfield!(p,S,x) #generic fallback
Base.setproperty!(p::Square, ::Val{:area},x) = setfield!(p,:side,√x)
Base.setproperty!(p::Square, ::Val{:diagonal},x) = setfield!(p,:side,x/√2)

julia> s = Square(√2)
Square(1.4142135623730951)

julia> s = Square(√2)^C

julia> s.diagonal
2.0000000000000004

julia> s.area=4
4

julia> s.diagonal
2.8284271247461903

julia> 

```

It is also easier to maintain this way and you can easily add up more implicit properties without stressing your memory too much.

---

_[View the full topic](https://discourse.julialang.org/t/parametric-field-names/56428)._
