# Fields of a struct

**URL:** https://discourse.julialang.org/t/fields-of-a-struct/107664
**Category:** General Usage
**Tags:** question
**Created:** [December 15, 2023, 1:10pm UTC](https://discourse.julialang.org/t/fields-of-a-struct/107664 "2023-12-15T13:10:48Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)
#### Post date: [December 15, 2023, 1:10pm UTC](https://discourse.julialang.org/t/fields-of-a-struct/107664/1 "2023-12-15T13:10:48Z")

</div>

```julia
julia> fieldnames(typeof(state))
(:hamiltonian, :replicas, :maxlength, :r_strat, :s_strat, :τ_strat, :post_step, :replica)

julia> state.step
5000

julia> state.laststep
5000

```

It is strange that while the variable ‘state’ as a datatype has no field name ‘step’, it does has a value for ‘state.step’.

What is wrong?

---

<div class="post-metadata">

### Author: ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)
#### Post date: [December 15, 2023, 1:16pm UTC](https://discourse.julialang.org/t/fields-of-a-struct/107664/2 "2023-12-15T13:16:01Z")

</div>

Take a look at `propertynames`. Note that it expects an instance of a type and not a type.

---

<div class="post-metadata">

### Author: ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)
#### Post date: [December 15, 2023, 1:38pm UTC](https://discourse.julialang.org/t/fields-of-a-struct/107664/3 "2023-12-15T13:38:17Z")

</div>

```julia
julia> propertynames(state)
(:hamiltonian, :replicas, :maxlength, :r_strat, :s_strat, :τ_strat, :post_step, :replica)

julia> fieldnames(typeof(state))
(:hamiltonian, :replicas, :maxlength, :r_strat, :s_strat, :τ_strat, :post_step, :replica)

```

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [December 15, 2023, 1:41pm UTC](https://discourse.julialang.org/t/fields-of-a-struct/107664/4 "2023-12-15T13:41:25Z")

</div>

Seems like whoever defined the type that `state` has overloaded `getproperty`, but didn’t make that known through `propertynames` (which they should). Open an issue at the package repo in question.

---

<div class="post-metadata">

### Author: ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)
#### Post date: [December 15, 2023, 1:42pm UTC](https://discourse.julialang.org/t/fields-of-a-struct/107664/5 "2023-12-15T13:42:54Z")

</div>

```julia
# Allow setting step, laststep, dτ, shift from QMCState.
function Base.getproperty(state::QMCState, key::Symbol)
    if key == :step
        step = state.replicas[1].params.step
        return step
    elseif key == :laststep
        laststep = state.replicas[1].params.laststep
        return laststep
    elseif key == :maxlength
        return getfield(state, :maxlength)[]
    elseif key == :dτ
        return state.replicas[1].params.dτ
    elseif key == :shift
        return state.replicas[1].params.shift
    else
        return getfield(state, key)
    end
end
function Base.setproperty!(state::QMCState, key::Symbol, value)
    if key == :step
        for r in state.replicas
            r.params.step = value
        end
        return value
    elseif key == :laststep
        for r in state.replicas
            r.params.laststep = value
        end
        return value
    elseif key == :dτ
        for r in state.replicas
            r.params.dτ = value
        end
        return value
    elseif key == :shift
        for r in state.replicas
            r.params.shift = value
        end
        return value
    elseif key == :maxlength
        getfield(state, :maxlength)[] = value
        return value
    else
        # This will error
        return setfield!(state, key, value)
    end
end

```

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [December 15, 2023, 2:03pm UTC](https://discourse.julialang.org/t/fields-of-a-struct/107664/6 "2023-12-15T14:03:27Z")

</div>

you should also have:

```julia
Base.propertynames(::QMCState) = (:step, :laststep, :maxlength, :dr, :shift)
```
