Initiate a struct without its name but only by its type in local scope

Try this:

function cpStructWithChangedField(inStruct, chField::Symbol, newValue)
    # type of struct
    inType = typeof(inStruct)
 
    # field count and names
    fc = fieldcount(inType)
    fn = fieldnames(inType)

    args = (field_name == chField ? newValue : getfield(inStruct, field_name)
            for field_name in fn)
    return inType(args...)
end
1 Like