# Anonymous functions in Dictionary with generator syntax

**URL:** https://discourse.julialang.org/t/anonymous-functions-in-dictionary-with-generator-syntax/15260
**Category:** General Usage
**Created:** [September 21, 2018, 1:55am UTC](https://discourse.julialang.org/t/anonymous-functions-in-dictionary-with-generator-syntax/15260 "2018-09-21T01:55:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [September 21, 2018, 1:55am UTC](https://discourse.julialang.org/t/anonymous-functions-in-dictionary-with-generator-syntax/15260/1 "2018-09-21T01:55:00Z")

</div>

I am running julia 1.0 on Manjaro Linux and have created a Dictionary like this:

```julia
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:

```julia
d["c"] = s -> "something else"

```

but I keep getting this error:

```julia
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:

```julia
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:

```julia
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!

---

<div class="post-metadata">

### Author: ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)
#### Post date: [September 21, 2018, 2:03am UTC](https://discourse.julialang.org/t/anonymous-functions-in-dictionary-with-generator-syntax/15260/2 "2018-09-21T02:03:14Z")

</div>

The simplest thing is to use `Function` instead of `Any`. It won’t really help performance, but at least is a bit more specific about the intent.

If the dictionary is going to contain many totally different functions, then given code like `d[x](s)`, the compiler doesn’t know what function will be called, precluding many optimizations. With the first comprehension, we know all the functions are “similar enough” that they share code, so some optimizations are possible.

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [September 21, 2018, 3:52am UTC](https://discourse.julialang.org/t/anonymous-functions-in-dictionary-with-generator-syntax/15260/3 "2018-09-21T03:52:35Z")

</div>

Thanks for the fast response!  
Makes sense, seems like the `Function`-Type is also what’s assigned in the example without the generator syntax. Just wondering why the compiler can’t do the same with this syntax.

---

<div class="post-metadata">

### Author: ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)
#### Post date: [September 28, 2018, 2:50pm UTC](https://discourse.julialang.org/t/anonymous-functions-in-dictionary-with-generator-syntax/15260/4 "2018-09-28T14:50:49Z")

</div>

Functions created by different expressions will have different types. For `s->f(s,"a")` and `s->f(s,"b")` to somehow share code, we would have to analyze them and prove that they are similar enough to share code. So far, that is too much to ask of the compiler. You can work around that by arranging for the functions to come from the same `->` expression:

```julia
get_function(str) = s->somefunction(s, str)

get_function("a"), get_function("b")

```
