I am currently writing a library that for a number of applications needs to take a dictionary of matrices as arguments. In order to make the functions reasonably general, I wanted to make the key/value types in the function arguments abstract. e.g. something like:
function print_dict(dict::AbstractDict{<:Real, AbstractArray{<:Real, 2}})
print(dict)
end
(This is not a real function I want to write, just for demonstration of the argument type)
Calling this function with a dict
of what I assumed to be a valid subtype of the function argument
dict = Dict{Int, Array{Float32, 2}}()
dict[1] = [10.3 3.4 ; 2.3 4]
print_dict(dict)
however gives the following error:
ERROR: LoadError: MethodError: no method matching print_dict(::Dict{Int64,Array{Float32,2}})
Closest candidates are:
print_dict(::AbstractDict{var"#s1",AbstractArray{var"#s2",2} where var"#s2"<:Real} where var"#s1"<:Real) at ***/dict_weirdness.jl:1
Stacktrace:
[1] top-level scope at ***/dict_weirdness.jl:10
[2] include(::String) at ./client.jl:457
[3] top-level scope at REPL[1]:1
in expression starting at ***/dict_weirdness.jl:10
I expected this to work as you can do things like AbstractArray{<:Real, 2}
as functional argument types no problem.
Can anyone explain why this does not work, and perhaps suggest any workarounds / better practises?
Thanks in advance, Michael