This seems to only work due to essentially eliminating kwargs...
in the MWE. Consider the following change, that breaks it again:
Base.@kwdef mutable struct MyStruct
id::Int
value::Symbol = :fixed
notimportant::Int = -1
end
MyStruct(id::Int, value::String, kwargs...) = MyStruct(id=id, value=Symbol(value), kwargs...)
MyStruct(id=1, value="a") # this fails again
While this certainly works, it achieves something else and does not solve the initial problem. It converts the String
to a Symbol
ahead of construction of the struct
, instead of during. While this seems like a valid fix, consider the following:
- I have a large “data storage” (the
struct
), that is initialized using data parsed from a file (yaml, csv, …). I read inString
s. This is fairly efficient for most fields. Later on I come to a point where profiling shows that keeping a specific field asSymbol
makes sense (due the timeloss of doingSymbol("somestring")
after reading the data, usingSymbol
s can be worse thanString
s for many fields). - I construct this
struct
at several points of my code. If I need to “symbolify” a new field, I need to change this everywhere instead of at a single central point (the constructor).