# \`nfields\` vs \`fieldcount\`

**URL:** <https://discourse.julialang.org/t/nfields-vs-fieldcount/33084>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 8, 2020, 4:31am UTC](https://discourse.julialang.org/t/nfields-vs-fieldcount/33084 "2020-01-08T04:31:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [January 8, 2020, 4:31am UTC](https://discourse.julialang.org/t/nfields-vs-fieldcount/33084/1 "2020-01-08T04:31:42Z")

</div>

What are the differences between `nfields` and `fieldcount`? Is

```julia
nfields(x) == fieldcount(typeof(x))

```

?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [January 8, 2020, 6:24am UTC](https://discourse.julialang.org/t/nfields-vs-fieldcount/33084/2 "2020-01-08T06:24:00Z")

</div>

Where you are working directly with structure of a type itself, `fieldcount` is appropriate and has related functions e.g. `fieldnames` `fieldtypes`. When you need to count of all the fields, including internal fields generally not intended for use outside of programming essential Julia features, `nfields` works given a Type or given a typed variable.

`nfields(::Type{T}) where {T}` works with many but not all types `T` (e.g. Singletons, empty structs).  
`nfields(x::T) where {T}` works given a realization of type `T`

`fieldcount(::Type{T}) where {T}` works given a type `T`  
`fieldcount(x::T) where {T}` throws an error.

Extending `Base.fieldcount(x::T) where {T} = fieldcount(T)` and then just using `fieldcount` is robust and efficient.

```julia
julia> two = 2; half = 1//2;
julia> struct OneField{T}
              x::T
         end;
julia> onefield = OneField("one")

julia> nfields(two) == nfields(Int) == 0;
julia> nfields(half) == nfields(Rational) == 2; 
julia> nfields(onefield) == nfields(OneField) == 1

julia> nfields(Int) == fieldcount(Int) == 0;
julia> nfields(Rational) == fieldcount(Rational);
julia> nfields(OneField) == fieldcount(OneField);

# fieldcount(x) where x is two or half or onefield throws and error

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 8, 2020, 7:25am UTC](https://discourse.julialang.org/t/nfields-vs-fieldcount/33084/3 "2020-01-08T07:25:00Z")

</div>

Also see this discussion:

[https://github.com/JuliaLang/julia/issues/29977](https://github.com/JuliaLang/julia/issues/29977)

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [January 8, 2020, 7:38am UTC](https://discourse.julialang.org/t/nfields-vs-fieldcount/33084/4 "2020-01-08T07:38:52Z")

</div>

> [@JeffreySarnoff](#):
>
> `nfields(::Type{T}) where {T}` works given a type `T`

This is incorrect. Look at the example mentioned in the issue @Tamas_Papp linked above:

```julia
julia> struct Foo end

julia> nfields(Foo)
21

```

Another difference is that inference sometimes works only with `nfields`. (Maybe that’s because `nfields` is a built-in function rather than a generic function?)

```julia
julia> f(x) = Val(nfields(x))
f (generic function with 1 method)

julia> g(::T) where {T} = Val(fieldcount(T))
g (generic function with 1 method)

julia> @inferred f((a=1,b=2,c=3))
Val{3}()

julia> @inferred g((a=1,b=2,c=3))
ERROR: return type Val{3} does not match inferred return type Val{_A} where _A
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] top-level scope at REPL[53]:1

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [January 8, 2020, 8:36am UTC](https://discourse.julialang.org/t/nfields-vs-fieldcount/33084/5 "2020-01-08T08:36:40Z")

</div>

Thanks for the additional insight @Tamas_Papp @tkf. I have revised my answer therewith.
