# Accessing data in a closure?

**URL:** <https://discourse.julialang.org/t/accessing-data-in-a-closure/54930>\
**Category:** New to Julia\
**Tags:** closure\
**Created:** [February 9, 2021, 2:35pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930 "2021-02-09T14:35:16Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [February 9, 2021, 2:35pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930/1 "2021-02-09T14:35:16Z")

</div>

Learning to work with closures. This is a construct to find out what is possible.

```julia
foo(d, x, y) = d[x] = y

struct Test
       f::Function
       Test(d::Dict, f::Function = (x, y) -> foo(d, x, y)) = new(f)
end

t = Test(Dict())

```

Whenever I call t.f(x, y) a key, value pair is added to the dict I passed with the Test constructor. So far, so good.

My question is: having t, can I access that dict somehow?

Thanks,  
Stef

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [February 9, 2021, 2:46pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930/2 "2021-02-09T14:46:58Z")

</div>

Found it!

```julia
t.f.d

```

I love this language!

I’m just leaving it here in case anyone else is interested.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 9, 2021, 4:12pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930/3 "2021-02-09T16:12:18Z")

</div>

I took me a while understanding what you did there. Interesting. I would have written it like this:

```julia
foo(d, x, y) = d[x] = y

struct Test
       f::Function
end

Test(d::Dict) = Test( (x,y) -> foo(d,x,y) )

t = Test(Dict())

@show t

@show t.f(1,2)

@show t.f 

@show t.f.d

```

I am not completely sure why you can access the dictionary as if it was a field of the closure. I guess that is because closures are implemented functors, meaning that it is something implicitly like

```julia
struct F
  d :: Dict
end

foo(d,x,y) = d[x] = y

(f::F)(x,y) = foo(f.d,x,y)

f = F(Dict())

@show f
@show f(1,2)
@show f.d 

```

Where clearly `f` has a `d` field. But I am not sure. That is not a common pattern to be seen (or is it?)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 9, 2021, 5:01pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930/4 "2021-02-09T17:01:52Z")

</div>

It is an implementation detail that variables captured by a closure can be accessed as fields of the closure object. Ideally, you should not rely on it.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 9, 2021, 5:23pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930/5 "2021-02-09T17:23:43Z")

</div>

> [@Henrique\_Becker](#):
>
> It is an implementation detail that variables captured by a closure can be accessed as fields of the closure object.

What would be the minimal working example of that? This does not work, for example:

```julia
a = 2
f = x -> a*x
@show f(1).a

```

Maybe that is a good opportunity to understand how closures work a little further.

Edit:

This works:

```julia
f(a,x) = x -> a*x
@show f(1,2).a 

```

Yet the analogy with the OP example does not seem exact in this case.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 9, 2021, 5:40pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930/6 "2021-02-09T17:40:52Z")

</div>

> [@lmiq](#):
>
> What would be the minimal working example of that?

```julia
julia> adder(x) = y -> x + y
adder (generic function with 1 method)

julia> adder(10).x
10

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 9, 2021, 5:42pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930/7 "2021-02-09T17:42:15Z")

</div>

> [@lmiq](#):
>
> `f(a,x) = x -> a*x`

You probably should not be taking that second argument. It is immediately shadowed by the inner closure parameter name.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 9, 2021, 6:14pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930/8 "2021-02-09T18:14:26Z")

</div>

> [@Henrique\_Becker](#):
>
> You probably should not be taking that second argument.

Yes, thanks. I added that trying to build a “standalone” working closure. But that makes no sense.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 9, 2021, 6:23pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930/9 "2021-02-09T18:23:37Z")

</div>

You may find the function `dump` useful in understanding new structures, the closure in this case.

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [February 9, 2021, 7:35pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930/10 "2021-02-09T19:35:46Z")

</div>

I don’t think

> [@Henrique\_Becker](#):
>
> It is an implementation detail that variables captured by a closure can be accessed as fields of the closure object. Ideally, you should not rely on it.

I don’t think that’s true. The docs say: “A closure is simply a callable object with field names corresponding to captured variables”, though there’s no code example of accessing it that way. ([Julia Functions · The Julia Language](https://docs.julialang.org/en/v1/devdocs/functions/#Closures)).  
This easy access to closed-over variable is one of the things I was happy to find in Julia, since I always felt was missing in Python. I use it quite a lot.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 9, 2021, 7:40pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930/11 "2021-02-09T19:40:58Z")

</div>

You are looking at the Developer’s Documentation, not the User’s Manual.

Accessing captured variables as fields in a closure object **is** an implementation detail, we have [word of god](https://tvtropes.org/pmwiki/pmwiki.php/Main/WordOfGod) on this:

> [@Function factories or callable structs?](https://discourse.julialang.org/t/function-factories-or-callable-structs/52987/7):
>
> 100% with one caveat: accessing the fields of a closure is not technically a stable part of the language’s API, so if you need to access the `a` field then you should use the explicit struct approach since that is guaranteed to work no matter what changes are made to the compiler. If you don’t need to access the `a` field then it’s just a matter of preference.

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [February 9, 2021, 8:30pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930/12 "2021-02-09T20:30:00Z")

</div>

Yeah, you’re right. I was searching the docs for “closure” and didn’t notice the result was in the Developer section.  
That’s pretty disappointing, cause I think it’s an awesome feature.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 9, 2021, 8:38pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930/13 "2021-02-09T20:38:27Z")

</div>

Feel free to use it as long as you’re willing to possibly have your code break with new Julia versions.
