Struct field name validate

Hellow!

I am looking for a reliable method to validate structure field names. Structure code is generated based on user input.

I think about eval(), but…

x = :else
exp = :($x = "Oops!"; print($x))
eval(exp)  # -> Oops!

then i tried using Base.isidentifier(), but

Base.isidentifier(:else) # -> thue

this is not what i need.

If i will checking against a list of registered names, i will need a supporting this list.

May be…

try
    include("generated_field_name_test.jl")
catch e
    # ...
end
julia> struct A     
         field_a    
       end          
                    
julia> fieldnames(A)
(:field_a,)         

Though I’d recommend checking the field names before creating the struct.

There is also the function hasfield, although I’m not entirely confident from your example code that that’s what you’re looking for: hasfield(Complex{Float64}, :re).