# Anonymous function in function closure

**URL:** <https://discourse.julialang.org/t/anonymous-function-in-function-closure/37504>\
**Category:** New to Julia\
**Tags:** closure\
**Created:** [April 13, 2020, 1:13pm UTC](https://discourse.julialang.org/t/anonymous-function-in-function-closure/37504 "2020-04-13T13:13:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![egcjm](https://avatars.discourse-cdn.com/v4/letter/e/5fc32e/32.png) [@egcjm](https://discourse.julialang.org/u/egcjm)\
**Post date:** [April 13, 2020, 1:13pm UTC](https://discourse.julialang.org/t/anonymous-function-in-function-closure/37504/1 "2020-04-13T13:13:35Z")

</div>

Consider the following simple code:

```julia
function myclass(x)
        h() = 1 + x
        g() = 2 + x
        () -> (h,g)
end

```

I understand

```julia
x = 1    
a = myclass(x)
a() # (>h, >g)
a()[1]() == 2 # = true as this is h()

```

but why

`a.h() == 2 # true`

which is how the above “class” is meant to be used?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 13, 2020, 1:32pm UTC](https://discourse.julialang.org/t/anonymous-function-in-function-closure/37504/2 "2020-04-13T13:32:12Z")

</div>

You’re seeing an effect of [WIP: redesign closures, then generic functions by JeffBezanson · Pull Request #13412 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/13412) which redesigned closures to be `struct`s with fields corresponding to the closed-over values.

When you write:

```julia
julia> let
         a = 1
         f() = a + 1
         @show f.a
         @show f()
       end
f.a = 1
f() = 2

```

the compiler transforms it into (roughly):

```julia
julia> let
         a = 1
         
         # The anonymous funcion is turned into a `struct`
         struct Anonymous1{T}
           a::T
         end
         # Make intances of that `struct` type callable
         (anon::Anonymous1)() = anon.a + 1
         # Make `f` an instance of that `struct` type
         f = Anonymous1(a)
         @show f.a
         @show f()
       end
f.a = 1
f() = 2

```

But the other thing I would say is: don’t use this to implement OOP. Multiple dispatch is a really nice way to organize code, and it’s worth learning the way Julia is intended to be used rather than trying to stick all of your functions inside your types.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 13, 2020, 1:41pm UTC](https://discourse.julialang.org/t/anonymous-function-in-function-closure/37504/3 "2020-04-13T13:41:40Z")

</div>

Do you know why the `let` block is needed to make your example work? I.e. what’s different at global scope?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 13, 2020, 2:10pm UTC](https://discourse.julialang.org/t/anonymous-function-in-function-closure/37504/4 "2020-04-13T14:10:41Z")

</div>

At global scope, the closure doesn’t capture the value of `a` but instead just references the global variable directly:

```julia
julia> a = 1
1

julia> f() = a + 1
f (generic function with 1 method)

julia> a = 5
5

julia> f()
6

```

Looking at the `code_lowered` shows that the body of `f()` is accessing `Main.a`:

```julia
julia> @code_lowered f()
CodeInfo(
1 ─ %1 = Main.a + 1
└── return %1
)

```

It is indeed surprising that the capture behavior is so different at global vs. local scope.
