I am trying to write a function that receives a tuple and a dictionary as arguments.
function findBestAction(state::Tuple{Int64, Int64}, qTable::Dict{Any, Matrix{Float64}})
doSomething()
end
I want the function to receive dictionaries whose keys can be of any possible type. I run the following command but receive error messages:
findBestAction((0, 0), qTable) #qTable::Dict{String, Matrix{Float64}}
Error Messages:
Stacktrace:
[1] top-level scope
@ e:\Master Thesis\lu_jizhou\Learning\q_learning.jl:33
ERROR: MethodError: no method matching findBestAction(::Tuple{Int64, Int64}, ::Dict{String, Matrix{Float64}})
Closest candidates are:
findBestAction(::Tuple{Int64, Int64}, ::Dict{Any, Matrix{Float64}}) at e:\Master Thesis\lu_jizhou\Learning\q_learning.jl:33
Stacktrace:
[1] top-level scope
@ e:\Master Thesis\lu_jizhou\Learning\q_learning.jl:48
How should I do that?
This is because Dict{String, ...) is not a subtype of Dict{Any, ...) .
This is the way parametric types (ie types with other types in their curly braces) behave in Julia. From the Julia manual section on Parametric types :
julia> Point{Float64} <: Point{Real} falseWarning
This last point is very important: even though
Float64 <: Realwe DO NOT havePoint{Float64} <: Point{Real}.In other words, in the parlance of type theory, Julia's type parameters are invariant
You can instead write the function as
function findBestAction(state::Tuple{Int64, Int64}, qTable::Dict{<:Any, Matrix{Float64}})
doSomething()
end
which explicitly says that any subtype of Any is allowed as the key type in the Dict .
You can also consider changing the Dict to an AbstractDict , and the value type of it to be something like <:AbstractMatrix{<:Real} , if your code is written generically in a way that works for any AbstractMatrix and any Real subtype.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.