# Fieldnames for user-defined structs

**URL:** <https://discourse.julialang.org/t/fieldnames-for-user-defined-structs/108508>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 8, 2024, 11:04am UTC](https://discourse.julialang.org/t/fieldnames-for-user-defined-structs/108508 "2024-01-08T11:04:38Z")\
**Posts on this page:** 5\
**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:** [January 8, 2024, 11:04am UTC](https://discourse.julialang.org/t/fieldnames-for-user-defined-structs/108508/1 "2024-01-08T11:04:38Z")

</div>

```julia
julia> struct Foo
                  bar
                  baz::Int
                  qux::Float64
       end

julia> foo = Foo(1,2,3)
Foo(1, 2, 3.0)

julia> fieldnames(foo)
ERROR: MethodError: no method matching fieldnames(::Foo)

Closest candidates are:
  fieldnames(::Core.TypeofBottom)
   @ Base reflection.jl:190
  fieldnames(::Type{<:Tuple})
   @ Base reflection.jl:192
  fieldnames(::DataType)
   @ Base reflection.jl:187
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[12]:1

```

This example is from the julia docs,  
[https://web.mit.edu/julia\_v0.6.2/julia/share/doc/julia/html/en/manual/types.html#Composite-Types-1](https://web.mit.edu/julia_v0.6.2/julia/share/doc/julia/html/en/manual/types.html#Composite-Types-1)

It does not work as the doc states.

In my computer, the following works:

```julia
julia> fieldnames(Foo)
(:bar, :baz, :qux)

```

---

<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:** [January 8, 2024, 11:09am UTC](https://discourse.julialang.org/t/fieldnames-for-user-defined-structs/108508/2 "2024-01-08T11:09:02Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> This example is from the julia docs,  
> [https://web.mit.edu/julia\_v0.6.2/julia/share/doc/julia/html/en/manual/types.html#Composite-Types-1](https://web.mit.edu/julia_v0.6.2/julia/share/doc/julia/html/en/manual/types.html#Composite-Types-1)

you’re reading the docs from julia version 0.6. Please use a modern version, e.g. [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#Composite-Types)

---

<div class="post-metadata">

**Author:** ![Maucejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maucejo/32/39090_2.png) [@Maucejo](https://discourse.julialang.org/u/Maucejo)\
**Post date:** [January 8, 2024, 11:23am UTC](https://discourse.julialang.org/t/fieldnames-for-user-defined-structs/108508/3 "2024-01-08T11:23:09Z")

</div>

This is the expected behavior. `foo` is actually an instance of the `Foo` struct. So, the fieldnames are attached to the struct and not to a particular instance.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 8, 2024, 11:23am UTC](https://discourse.julialang.org/t/fieldnames-for-user-defined-structs/108508/4 "2024-01-08T11:23:35Z")

</div>

As so often, the help is your friend:

```julia
help?> fieldnames

  fieldnames(x::DataType)

  Get a tuple with the names of the fields of a DataType.

```

`fieldnames` takes a type, not an instance of a type as its argument:

```julia
julia> fieldnames(Foo)
(:bar, :baz, :qux)

julia> foo = Foo(1,2,3); fieldnames(typeof(foo))
(:bar, :baz, :qux)

```

---

<div class="post-metadata">

**Author:** ![rmsmsgood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rmsmsgood/32/20544_2.png) [@rmsmsgood](https://discourse.julialang.org/u/rmsmsgood)\
**Post date:** [January 8, 2024, 11:51am UTC](https://discourse.julialang.org/t/fieldnames-for-user-defined-structs/108508/5 "2024-01-08T11:51:35Z")

</div>

And there is `propertynames` for instance. It’s more useful than `fieldnames` which is for structure.
