Behavior of `fieldtypes` applied to `Tuple{...}`

Here is the docstring for fieldtypes:

fieldtypes(T::Type)

The declared types of all fields in a composite DataType T as a tuple.

Examples
≡≡≡≡≡≡≡≡≡≡

julia> struct Foo
           x::Int64
           y::String
       end
  
julia> fieldtypes(Foo)
(Int64, String)

As far as I can tell, this only seems to address composite types defined with the struct keyword. However, it so happens that fieldtypes performs a useful operation on a Tuple{...} type:

julia> fieldtypes(Tuple{Int, String})
(Int64, String)

It doesn’t seem like this behavior is really covered by the current docstring. So, my question is, is the behavior of fieldtypes when applied to a Tuple{...} type considered public API that we can rely on?

There are a few related unit tests in test/reflection.jl, like these:

@test fieldtype(Tuple{Vararg{Int8}}, 1) === Int8
@test fieldtype(Tuple{Vararg{Int8}}, 10) === Int8

@test fieldtype((NamedTuple{T,Tuple{Int,String}} where T), 1) === Int
@test fieldtype((NamedTuple{T,Tuple{Int,String}} where T), 2) === String

@test fieldtypes((NamedTuple{T,Tuple{Int,String}} where T)) === (Int, String)

So, that provides some hope. But there are no unit tests for the simple tuple type case like fieldtypes(Tuple{Int, String}).

1 Like

Besides fieldtypes, the functions fieldtype and Base.datatype_nfields also work for Tuple. It would be great to see this reflected in the documentation.

1 Like