Recursive `fieldnames()`?

question

#1

Is there a way to apply fieldnames() resursively so that all fieldnames can be printed in cascade? I know that dump() sort of does this, but it prints literally everything - it’s hard to pick ham from peas.


#2

Maybe there already exists a method for this.

If not, you could write your own. Here’s an example:

function rfieldnames(::Type{T}, i::Int=0; recursive::Bool=true) where T
    fields = fieldnames(T)
    if !isempty(fields)
        for field in fields
            println("    "^i, field, "::", fieldtype(T, field))
            if recursive
                rfieldnames(fieldtype(T, field), i+1, recursive=true)
            end
        end
    end
end

#3

Thanks. I wish I’m so versed like you to write down the snippet in milliseconds.

It’ll be thrilling if a recursion function can be added to fieldnames.