Am I coding wrong or should I open an issue ?
BSON cannot load an struct that has an array of a subtype field.
julia> using BSON
julia> mutable struct Bar
arrreal::Array{<:Real,1}
end
julia> bar = Bar(Real[])
Bar(Real[])
julia> BSON.@save "bar.bson" bar
julia> BSON.@load "bar.bson"
ERROR: MethodError: no method matching Array{#s277,1} where #s277<:Real(::Array{Any,1})
Stacktrace:
[1] convert(::Type{Array{#s277,1} where #s277<:Real}, ::Array{Any,1}) at .\array.jl:490
[2] newstruct!(::Bar, ::Array{Any,1}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\extensions.jl:98
[3] newstruct_raw(::IdDict{Any,Any}, ::Type, ::Dict{Symbol,Any}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\extensions.jl:125
[4] (::BSON.var"#45#46")(::Dict{Symbol,Any}, ::IdDict{Any,Any}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\extensions.jl:140
[5] raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\read.jl:88
[6] (::BSON.var"#20#22"{IdDict{Any,Any}})(::Dict{Symbol,Any}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\read.jl:82
[7] applychildren!(::BSON.var"#20#22"{IdDict{Any,Any}}, ::Dict{Symbol,Any}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\BSON.jl:21
[8] _raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\read.jl:82
[9] raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\read.jl:89
[10] raise_recursive at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\read.jl:99 [inlined]
[11] load(::String) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\read.jl:104
[12] top-level scope at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\BSON.jl:52
It does not happen if I instanciate with an array of concrete type.
julia> bar = Bar(Int[])
Bar(Int64[])
julia> BSON.@save "bar.bson" bar
julia> BSON.@load "bar.bson"
It also happens when the array is not empty.
julia> bar = Bar(Real[1, 1.0])
Bar(Real[1, 1.0])
julia> BSON.@save "bar.bson" bar
julia> BSON.@load "bar.bson"
ERROR: MethodError: no method matching Array{#s277,1} where #s277<:Real(::Array{Any,1})
Stacktrace:
[1] convert(::Type{Array{#s277,1} where #s277<:Real}, ::Array{Any,1}) at .\array.jl:490
[2] newstruct!(::Bar, ::Array{Any,1}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\extensions.jl:98
[3] newstruct_raw(::IdDict{Any,Any}, ::Type, ::Dict{Symbol,Any}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\extensions.jl:125
[4] (::BSON.var"#45#46")(::Dict{Symbol,Any}, ::IdDict{Any,Any}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\extensions.jl:140
[5] raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\read.jl:88
[6] (::BSON.var"#20#22"{IdDict{Any,Any}})(::Dict{Symbol,Any}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\read.jl:82
[7] applychildren!(::BSON.var"#20#22"{IdDict{Any,Any}}, ::Dict{Symbol,Any}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\BSON.jl:21
[8] _raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\read.jl:82
[9] raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any}) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\read.jl:89
[10] raise_recursive at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\read.jl:99 [inlined]
[11] load(::String) at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\read.jl:104
[12] top-level scope at C:\Users\Henri\.julia\packages\BSON\Ryxwc\src\BSON.jl:52
I don’t think using Array{<:Real,1}
would pose problem for anything else. I guess using a parametric type would solve the problem but that is somewhat annoying to do in my actual use case.