I am running julia 1.0 on Manjaro Linux and have created a Dictionary like this:
d = Dict( i => ( s -> somefunction(s,i) ) for i in ["a", "b"])
where somefunction
always returns a string.
Now I want to add another entry to d
, so I tried this:
d["c"] = s -> "something else"
but I keep getting this error:
ERROR: MethodError: Cannot `convert` an object of type getfield(Main, Symbol("##84#85")) to an object of type getfield(Main, Symbol("##81#83")){String}
Closest candidates are:
convert(::Type{T}, ::T) where T at essentials.jl:154
Stacktrace:
[1] setindex!(::Dict{String,getfield(Main, Symbol("##81#83")){String}}, ::Function, ::String) at ./dict.jl:381
[2] top-level scope at none:0
From my understanding it must have something to do with the way Julia assigns types to anonymous functions. I’ve experimented a bit and the following thing does work:
d = Dict{String,Any}( i => ( s -> somefunction(s,i) ) for i in ["a", "b"])
d["c"] = s -> "something else"
It also works flawlessly if I don’t use the generator syntax, but which is obviously not fit for larger arrays:
d = Dict( "a" => ( s -> somefunction(s,"a") ), "b" => ( s -> somefunction(s,"b") ))
Still, I can’t help but think there must be a better way, especially since I’d think that the Type Any
for the Dictionary doesn’t leave much room for optimizations by the compiler. Is there a more elegant way to do this, maybe a type all of those abstract functions are a subtype of?
Thanks in advance!