# Private fields with \`fieldnames\` and \`propertynames\`

**URL:** <https://discourse.julialang.org/t/private-fields-with-fieldnames-and-propertynames/98442>\
**Category:** General Usage\
**Created:** [May 7, 2023, 6:34pm UTC](https://discourse.julialang.org/t/private-fields-with-fieldnames-and-propertynames/98442 "2023-05-07T18:34:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sirion34](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sirion34/32/49526_2.png) [@sirion34](https://discourse.julialang.org/u/sirion34)\
**Post date:** [May 7, 2023, 6:34pm UTC](https://discourse.julialang.org/t/private-fields-with-fieldnames-and-propertynames/98442/1 "2023-05-07T18:34:57Z")

</div>

everywhere write that with the help of `propertynames` I can set public and private fields, but how legal is it to do this with `fieldnames`? Struct the structure stores its fields in Struct\*.name.names and I can overload `fieldnames`, so I can redefine `fieldnames` however I want?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [May 7, 2023, 6:39pm UTC](https://discourse.julialang.org/t/private-fields-with-fieldnames-and-propertynames/98442/2 "2023-05-07T18:39:21Z")

</div>

You cannot redefine `fieldnames` or `getfield`, those are two of the very few `builtin` functions in julia which cannot be extended or changed by users.

---

<div class="post-metadata">

**Author:** ![sirion34](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sirion34/32/49526_2.png) [@sirion34](https://discourse.julialang.org/u/sirion34)\
**Post date:** [May 7, 2023, 7:08pm UTC](https://discourse.julialang.org/t/private-fields-with-fieldnames-and-propertynames/98442/3 "2023-05-07T19:08:06Z")

</div>

in julia 1.8 I can:

```julia
julia> struct Foo
           a::Int64
           b::String
       end

julia> fieldnames(Foo)
(:a, :b)

julia> Base.fieldnames(::Type{Foo}) = (:a,)

julia> fieldnames(Foo)
(:a,)

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [May 7, 2023, 7:09pm UTC](https://discourse.julialang.org/t/private-fields-with-fieldnames-and-propertynames/98442/4 "2023-05-07T19:09:49Z")

</div>

Oh right, my mistake, it’s just `getfield`, not `fieldnames` I suppose. I would strongly recommend against overloading that, I don’t think you’re supposed to do it and you could break things.

---

<div class="post-metadata">

**Author:** ![sirion34](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sirion34/32/49526_2.png) [@sirion34](https://discourse.julialang.org/u/sirion34)\
**Post date:** [May 7, 2023, 7:15pm UTC](https://discourse.julialang.org/t/private-fields-with-fieldnames-and-propertynames/98442/5 "2023-05-07T19:15:07Z")

</div>

Then how to change the output list of fields in a type? (I only have type information, I can’t use `propertynames`)

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [May 7, 2023, 7:20pm UTC](https://discourse.julialang.org/t/private-fields-with-fieldnames-and-propertynames/98442/6 "2023-05-07T19:20:40Z")

</div>

Just don’t do it.
