to fix based on your code, the minimal modification is:
julia> Array{MyStruct,1}([MyStruct("great",2,"nice")])
1-element Array{MyStruct,1}:
MyStruct("great", 2, "nice")
The reason is that the Array{T,N}
constructor wants the argument to be an array with eltype
being T
, I understand this is not the easiest code to write, so I wonder if there’s better way.
But, I noticed you can ‘hack’ the Base.convert
a little:
julia> Base.convert(MyStruct, x::AbstractArray) = MyStruct(x...)
julia> Array{MyStruct,1}([["great",2,"nice"], ["hmm", 3, "hacky"]])
2-element Array{MyStruct,1}:
MyStruct("great", 2, "nice")
MyStruct("hmm", 3, "hacky")
which, at this point you might as well just write:
MyStruct(x::Tuple) = new(x...) #inner constructor inside `struct`
julia> MyStruct.([("great",2,"nice"), ("hmm", 3, "hacky")])
2-element Array{MyStruct,1}:
MyStruct("great", 2, "nice")
MyStruct("hmm", 3, "hacky")