# Anonymous function passed as a string gets overwritten

**URL:** <https://discourse.julialang.org/t/anonymous-function-passed-as-a-string-gets-overwritten/97195>\
**Category:** New to Julia\
**Tags:** question, metaprogramming, dates, functions\
**Created:** [April 6, 2023, 9:15pm UTC](https://discourse.julialang.org/t/anonymous-function-passed-as-a-string-gets-overwritten/97195 "2023-04-06T21:15:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oliviermilla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliviermilla/32/47200_2.png) [@oliviermilla](https://discourse.julialang.org/u/oliviermilla)\
**Post date:** [April 6, 2023, 9:15pm UTC](https://discourse.julialang.org/t/anonymous-function-passed-as-a-string-gets-overwritten/97195/1 "2023-04-06T21:15:39Z")

</div>

Hello julians,

I face a little puzzle in Julia that I can’t figure out. The code is straightforward:

```julia
function tada(period::Union{Nothing,Type{T}}=nothing) where {T<:Period})
    key = Vector{Function}()
    p = Symbol(period)
    while true
        println("p: $(p)")
        func = getfield(Dates, p)
        pushfirst!(key, x -> Dates.value(func(x)))
        up = Symbol(Dates.coarserperiod(func)[1])
        if (up == p)
            break
        else
            p = up
        end
    end
    println("First: $(key[1](Dates.now())). Second: $(key[2](Dates.now()))")
end

```

The idea is that the user can pass a Period as argument (say `Dates.Month`) and I want to get an array of functions that apply all the coarser periods.

The `coarserperiod` returns the “above” period. (ex: `Dates.coarserperiod(Day)` returns Dates.Week and `Dates.coarserperiod(Month)` returns Dates.Year).

The above code prints this:

```julia
> tada(Dates.Month)
> p: Month
> p: Year
> First: 2023. Second: 2023

```

as you can see, the loop does find the right coarser periods, but the function that are stored in the array are the same while they should not.

What am I missing?

---

<div class="post-metadata">

**Author:** ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)\
**Post date:** [April 6, 2023, 9:34pm UTC](https://discourse.julialang.org/t/anonymous-function-passed-as-a-string-gets-overwritten/97195/2 "2023-04-06T21:34:56Z")

</div>

I tried your code with various Julia versions, but I always get the answer you expect.

```julia
julia> function tada(period::Union{Nothing,Type{T}}=nothing) where {T<:Period}
           key = Vector{Function}()
           p = Symbol(period)
           while true
               println("p: $(p)")
               func = getfield(Dates, p)
               pushfirst!(key, x -> Dates.value(func(x)))
               up = Symbol(Dates.coarserperiod(func)[1])
               if (up == p)
                   break
               else
                   p = up
               end
           end
           println("First: $(key[1](Dates.now())). Second: $(key[2](Dates.now()))")
       end
tada (generic function with 2 methods)

julia> tada(Month)
p: Month
p: Year
First: 2023. Second: 4

```

So there must be something else, too, that messes up your environment.

---

<div class="post-metadata">

**Author:** ![oliviermilla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliviermilla/32/47200_2.png) [@oliviermilla](https://discourse.julialang.org/u/oliviermilla)\
**Post date:** [April 6, 2023, 9:39pm UTC](https://discourse.julialang.org/t/anonymous-function-passed-as-a-string-gets-overwritten/97195/3 "2023-04-06T21:39:16Z")

</div>

Thank you @HanD. Hmm. I did spend some time debugging this! 😅

I have no idea how to debug the environment. 😕

But Thank you for looking into it. At least it’s not the code. 🙂

---

<div class="post-metadata">

**Author:** ![oliviermilla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliviermilla/32/47200_2.png) [@oliviermilla](https://discourse.julialang.org/u/oliviermilla)\
**Post date:** [April 6, 2023, 10:51pm UTC](https://discourse.julialang.org/t/anonymous-function-passed-as-a-string-gets-overwritten/97195/4 "2023-04-06T22:51:34Z")

</div>

Restarted julia. It works 😕
