# Overloading \`fieldnames()\` function

**URL:** <https://discourse.julialang.org/t/overloading-fieldnames-function/98387>\
**Category:** General Usage\
**Created:** [May 5, 2023, 3:32pm UTC](https://discourse.julialang.org/t/overloading-fieldnames-function/98387 "2023-05-05T15:32:16Z")\
**Posts on this page:** 8\
**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 5, 2023, 3:32pm UTC](https://discourse.julialang.org/t/overloading-fieldnames-function/98387/1 "2023-05-05T15:32:16Z")

</div>

Is it safe to overload the `fieldnames()` function if I want to hide some fields for a specific type?

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [May 5, 2023, 3:42pm UTC](https://discourse.julialang.org/t/overloading-fieldnames-function/98387/2 "2023-05-05T15:42:11Z")

</div>

Do you want to prevent people from accessing field names from an instance of a type or prevent them from accessing fieldnames from the type itself? If the former, you’ll want to overload `propertynames`. If the latter, I’d say probably not to do that. I don’t know if it’s actually unsafe, but it makes generic reconstruction (like what `@set` from Setfield.jl would have to do). And I don’t think it buys you any extra protection from users accessing the field because that will usually be happening through `propertynames`/`getproperty`/`setproperty!` on an instance rather than a type.

---

<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 5, 2023, 4:08pm UTC](https://discourse.julialang.org/t/overloading-fieldnames-function/98387/3 "2023-05-05T16:08:37Z")

</div>

First of all, nothing will break if I overload this function?

---

<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:** [May 5, 2023, 4:58pm UTC](https://discourse.julialang.org/t/overloading-fieldnames-function/98387/4 "2023-05-05T16:58:00Z")

</div>

Things might break, but trying to hide fields with that won’t really work - if you take away the ability to get a field of an object, you yourself also won’t be able to access it.

In general, there is no way to completely hide the internals of an object for the users of your code.

---

<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 5, 2023, 5:32pm UTC](https://discourse.julialang.org/t/overloading-fieldnames-function/98387/5 "2023-05-05T17:32:48Z")

</div>

what should i do to skip struct fields during serialization/deserialization?

---

<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:** [May 5, 2023, 5:46pm UTC](https://discourse.julialang.org/t/overloading-fieldnames-function/98387/6 "2023-05-05T17:46:54Z")

</div>

Write a custom serialization function on a struct-by-struct basis - though for more specific help, you’ll have to give some more information on what you’re trying to do.

---

<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 5, 2023, 6:15pm UTC](https://discourse.julialang.org/t/overloading-fieldnames-function/98387/7 "2023-05-05T18:15:57Z")

</div>

If I want do deserialize `Dict()` into struct, but some fields should not be deserialized (they are generated in the constructor based on the rest of the fields)

deserialization function:

```julia
function deser(
    ::CustomType,
    ::Type{D},
    data::AbstractDict{K,V},
)::D where {D<:Any,K<:Union{AbstractString,Symbol},V<:Any}
    vals = Union{fieldtypes(D)...}[]

    for (type, name) in zip(fieldtypes(D), fieldnames(D))

        ...

        push!(vals, res)
    end

    return D(vals...)
end

```

---

<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:** [May 5, 2023, 8:17pm UTC](https://discourse.julialang.org/t/overloading-fieldnames-function/98387/8 "2023-05-05T20:17:44Z")

</div>

I’d filter the names in that loop by checking whether you “want” them, otherwise `continue` the loop to process the next value.
