mauro3 / Parameters.jl

Types with default field values, keyword constructors and (un-)pack macros
Other
419 stars 31 forks source link

fieldnames extension of Parameter Struct #129

Closed stevengogogo closed 3 years ago

stevengogogo commented 3 years ago

Dear Developers,

I'm wondering that whether Base. fieldnames (or something similar) can be used to list all the fieldnames of the stuct of Parameter.jl.

The following is the command that I tried to get all the field names from the Base function


julia> using Parameters

julia> @with_kw struct foo
           a 
       end
foo

julia> f = foo(1)
foo
  a: Int64 1

julia> fieldnames(f)
ERROR: MethodError: no method matching fieldnames(::foo)
Closest candidates are:
  fieldnames(::Core.TypeofBottom) at reflection.jl:175
  fieldnames(::Type{var"#s9"} where var"#s9"<:Tuple) at reflection.jl:177
  fieldnames(::DataType) at reflection.jl:172
  ...
Stacktrace:
 [1] top-level scope at REPL[4]:1

julia> 

Probably using extension to make parameter.jl fit to Base.fieldnames is an option.

Something like


function Base.fieldnames(foo::StructofParameters)
    # To Do
end
stevengogogo commented 3 years ago

I found this works

julia> using Parameters

julia> @with_kw struct foo
                  a 
              end
foo

julia> fieldnames(foo)
(:a,)