Skip to content

Can not load dictionary with key::String and value::Function #129

@KronosTheLate

Description

@KronosTheLate

But I can write it without error, which is extra worrysome.

MWE:

julia> using BSON

julia> my_func(x) = x^2
my_func (generic function with 1 method)

julia> bson("test.bson", Dict([:my_func=>my_func])); BSON.load("test.bson")  # No issue with symbol
Dict{Symbol, Any} with 1 entry:
  :my_func => my_func

julia> bson("test.bson", Dict(["my_var"=>pi])); BSON.load("test.bson")  # No issue with string key, non-function value
Dict{String, Irrational{}} with 1 entry:
  "my_var" => π

julia> bson("test.bson", Dict(["my_func"=>my_func])); BSON.load("test.bson")  # But string key and funtion value? Error
ERROR: MethodError: no method matching typeof(my_func)()
Stacktrace:
  [1] (::BSON.var"#39#40")(d::Dict{Symbol, Any})
    @ BSON ~/.julia/packages/BSON/DOYqe/src/extensions.jl:102
  [2] _raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/DOYqe/src/read.jl:82
  [3] raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/DOYqe/src/read.jl:93
  [4] (::BSON.var"#45#46"{IdDict{Any, Any}, Module})(x::Dict{Symbol, Any})
    @ BSON ~/.julia/packages/BSON/DOYqe/src/extensions.jl:168
  [5] iterate
    @ ./generator.jl:47 [inlined]
  [6] collect_to!(dest::Vector{Vector{Any}}, itr::Base.Generator{Vector{Any}, BSON.var"#45#46"{IdDict{Any, Any}, Module}}, offs::Int64, st::Int64)
    @ Base ./array.jl:892
  [7] collect_to_with_first!(dest::Vector{Vector{Any}}, v1::Vector{Any}, itr::Base.Generator{Vector{Any}, BSON.var"#45#46"{IdDict{Any, Any}, Module}}, st::Int64)
    @ Base ./array.jl:870
  [8] _collect(c::Vector{Any}, itr::Base.Generator{Vector{Any}, BSON.var"#45#46"{IdDict{Any, Any}, Module}}, ::Base.EltypeUnknown, isz::Base.HasShape{1})
    @ Base ./array.jl:864
  [9] collect_similar(cont::Vector{Any}, itr::Base.Generator{Vector{Any}, BSON.var"#45#46"{IdDict{Any, Any}, Module}})
    @ Base ./array.jl:763
 [10] map(f::Function, A::Vector{Any})
    @ Base ./abstractarray.jl:3282
 [11] newstruct_raw(cache::IdDict{Any, Any}, T::Type, d::Dict{Symbol, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/DOYqe/src/extensions.jl:168
 [12] (::BSON.var"#49#50")(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/DOYqe/src/extensions.jl:184
 [13] raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/DOYqe/src/read.jl:92
 [14] raise_recursive
    @ BSON ~/.julia/packages/BSON/DOYqe/src/read.jl:103 [inlined]
 [15] load
    @ BSON ~/.julia/packages/BSON/DOYqe/src/read.jl:108 [inlined]
 [16] load(x::String)
    @ BSON ~/.julia/packages/BSON/DOYqe/src/read.jl:108
 [17] top-level scope
    @ REPL[19]:1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions