# Question about use of generated functions

**URL:** <https://discourse.julialang.org/t/question-about-use-of-generated-functions/3355>\
**Category:** General Usage\
**Created:** [April 24, 2017, 1:16pm UTC](https://discourse.julialang.org/t/question-about-use-of-generated-functions/3355 "2017-04-24T13:16:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [April 24, 2017, 1:16pm UTC](https://discourse.julialang.org/t/question-about-use-of-generated-functions/3355/1 "2017-04-24T13:16:58Z")

</div>

In a package of mines I need to often create an empty `Dict` to which I later add elements. In order to avoid always allocating a new empty `Dict`, I thought about exploiting generated functions in this way:

```julia
@generated foo(x) = Dict{x,x}()

```

So now `foo(0.0)` is very fast an cheaper than always running `Dict{Float64,Float64}()`. Is this considered a good use of generated functions? Or is it an abuse and it’s prone to fail at some point?

**Edit** : contrary to what I wrote in the first version of the message, I’m more interested in `Base.ImmutableDict` rather than `Dict`.

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [April 24, 2017, 1:41pm UTC](https://discourse.julialang.org/t/question-about-use-of-generated-functions/3355/2 "2017-04-24T13:41:52Z")

</div>

No, this way you create a single global `Dict{Float64,Float64}()` that all share the same data:

```julia
julia> @generated foo(x) = Dict{x,x}()
foo (generic function with 1 method)

julia> d1 = foo(0.0)
Dict{Float64,Float64} with 0 entries

julia> d2 = foo(0.0)
Dict{Float64,Float64} with 0 entries

julia> d1[1.0] = 2.0
2.0

julia> d2
Dict{Float64,Float64} with 1 entry:
  1.0 => 2.0

julia> d1 === d2
true

```

If you want multiple instances of a `Dict`, you have to create them at some point, and no language feature can change it.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 24, 2017, 1:55pm UTC](https://discourse.julialang.org/t/question-about-use-of-generated-functions/3355/3 "2017-04-24T13:55:33Z")

</div>

> [@dfdx](#):
>
> no language feature can change it.

We don’t guarantee that the generator is only run once. So yes the behavior can change.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [April 24, 2017, 2:01pm UTC](https://discourse.julialang.org/t/question-about-use-of-generated-functions/3355/4 "2017-04-24T14:01:57Z")

</div>

Sorry for the simplification, but actually I don’t use a `Dict` but a type very similar to `Base.ImmutableDict` which is immutable.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [April 24, 2017, 2:46pm UTC](https://discourse.julialang.org/t/question-about-use-of-generated-functions/3355/5 "2017-04-24T14:46:21Z")

</div>

`Base.ImmutableDict` doesn’t have the same problem as `Dict` (sorry, when writing the first message I didn’t consider I was simplifying too much):

```julia
julia> @generated foo(x) = Base.ImmutableDict{x,x}()
foo (generic function with 1 method)

julia> d1 = foo(0.0)
Base.ImmutableDict{Float64,Float64} with 0 entries

julia> d2 = foo(0.0)
Base.ImmutableDict{Float64,Float64} with 0 entries

julia> d1 = Base.ImmutableDict(d1, 3=>4)
Base.ImmutableDict{Float64,Float64} with 1 entry:
  3.0 => 4.0

julia> d2
Base.ImmutableDict{Float64,Float64} with 0 entries

```

The question still holds: is this use of `@generated` fine? I don’t care the body of the function isn’t run exactly once, the important is that it isn’t always.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 24, 2017, 3:15pm UTC](https://discourse.julialang.org/t/question-about-use-of-generated-functions/3355/6 "2017-04-24T15:15:06Z")

</div>

Embedding an object in the returned AST (or returning an object) is fine. Do note that the construction of the object cannot depend on any methods defined later, otherwise it’s fine.

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [April 24, 2017, 3:20pm UTC](https://discourse.julialang.org/t/question-about-use-of-generated-functions/3355/7 "2017-04-24T15:20:47Z")

</div>

If I understand you correctly, you can also just use a constant for it.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [April 24, 2017, 3:21pm UTC](https://discourse.julialang.org/t/question-about-use-of-generated-functions/3355/8 "2017-04-24T15:21:20Z")

</div>

Thank you so much. I don’t redefine the constructor method, so should be good.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [April 24, 2017, 3:23pm UTC](https://discourse.julialang.org/t/question-about-use-of-generated-functions/3355/9 "2017-04-24T15:23:00Z")

</div>

I don’t know the type of dictionary’s elements, for my use case it can be any `<:AbstractFloat`, that’s why I need a function.

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [April 24, 2017, 3:32pm UTC](https://discourse.julialang.org/t/question-about-use-of-generated-functions/3355/10 "2017-04-24T15:32:18Z")

</div>

> [@yuyichao](#):
>
> We don’t guarantee that the generator is only run once. So yes the behavior can change.

Just to clarify, I mean that if you need to have, say, 5 distinct (mutable) objects, you have to create 5 distinct objects. It’s technically impossible to allocate memory only for one of them, but fill it with 5 different data, regardless of the features available in the language.
