# Method Error : Not identifying Integer as Any

**URL:** https://discourse.julialang.org/t/method-error-not-identifying-integer-as-any/77957
**Category:** General Usage
**Created:** [March 16, 2022, 10:20am UTC](https://discourse.julialang.org/t/method-error-not-identifying-integer-as-any/77957 "2022-03-16T10:20:25Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![imantha](https://avatars.discourse-cdn.com/v4/letter/i/6a8cbe/32.png) [@imantha](https://discourse.julialang.org/u/imantha)
#### Post date: [March 16, 2022, 10:20am UTC](https://discourse.julialang.org/t/method-error-not-identifying-integer-as-any/77957/1 "2022-03-16T10:20:25Z")

</div>

Hi, I have function `update_agent_attributes` that accepts a `Dict{Symbol, Any}` as one of the arguments. However when I pass a `Dict{Symbol, Interger}` I get an `Method Error`. Any idea why ?

```julia
mutable struct Casualty
    id :: Int 
    pos :: Tuple{Int,Int}
    trauma :: Int 
    awaiting_rescue :: Bool
    rescued_by :: Int
end

mutable struct Rescuer
    id :: Int 
    pos :: Tuple{Int, Int}
end

@doc "Update agent attributes" ->
function update_agent_attributes!(agent::Union{Casualty,Rescuer},attr::Dict{Symbol,Any})
    for (k,v) in attr
        setfield!(agent,k,v)
    end 
end

c1 = Casualty(1, (7,7), 3, true, 999)
attrs = Dict(:awaiting_recue => false, :rescued_by => 6)
update_agent_attributes!(c1, attrs)

>>>
ERROR: MethodError: no method matching update_agent_attributes!(::Casualty, ::Dict{Symbol, Integer})
Closest candidates are:
  update_agent_attributes!(::Union{Casualty, Rescuer}, !Matched::Dict{Symbol, Any}) at ~/github/CRTT-lite/crtt_lite_osm.jl:120

```

---

<div class="post-metadata">

### Author: ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)
#### Post date: [March 16, 2022, 10:35am UTC](https://discourse.julialang.org/t/method-error-not-identifying-integer-as-any/77957/2 "2022-03-16T10:35:34Z")

</div>

A smaller MWE would be

```julia
julia> f(d::Dict{Int, Any}) = println(keys(d))

julia> f(Dict(1=>1, 2=>"a"))
[2, 1]

julia> f(Dict(1=>1, 2=>2))
ERROR: MethodError: no method matching f(::Dict{Int64, Int64})
Closest candidates are:
  f(::Dict{Int64, Any}) at REPL[3]:1
Stacktrace:
 [1] top-level scope
   @ REPL[4]:1

```

what you actually want is

```julia
julia> g(d::Dict{Int, T}) where T <: Any = println(keys(d))
g (generic function with 1 method)

julia> g(Dict(1=>1, 2=>"a"))
[2, 1]

julia> g(Dict(1=>1, 2=>2))
[2, 1]

```

As although Any sounds general, it is, in `f`’s context, a specific type of Dict, so only one method will be created - one that unboxes the Any of the values.

Whereas in `g`’s context, a method for each type T can be generated specialised on the type of T.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [March 16, 2022, 11:50am UTC](https://discourse.julialang.org/t/method-error-not-identifying-integer-as-any/77957/3 "2022-03-16T11:50:37Z")

</div>

> [@lawless-m](#):
>
> `g(d::Dict{Int, T}) where T <: Any = println(keys(d))`

Alternatively

```julia
g(d::Dict{Int, <:Any}) = println(keys(d))

```

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [March 16, 2022, 12:39pm UTC](https://discourse.julialang.org/t/method-error-not-identifying-integer-as-any/77957/4 "2022-03-16T12:39:01Z")

</div>

In essence, this comes down to [parametric types in julia being invariant, not covariant](https://docs.julialang.org/en/v1/manual/types/#man-parametric-composite-types). So even though we have `Int <: Any` we DO NOT have `Foo{Int} <: Foo{Any}`.
