# Fields of an anonymous function?

**URL:** <https://discourse.julialang.org/t/fields-of-an-anonymous-function/44562>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 8, 2020, 11:22am UTC](https://discourse.julialang.org/t/fields-of-an-anonymous-function/44562 "2020-08-08T11:22:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [August 8, 2020, 11:22am UTC](https://discourse.julialang.org/t/fields-of-an-anonymous-function/44562/1 "2020-08-08T11:22:00Z")

</div>

Looking at some examples of a package I came across this type of behavior:

```julia
function f(n)
    a = n
    x -> x^a
end

g = f(2)

g.a # 2

```

Can someone point out where that is documented?

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [August 8, 2020, 11:59am UTC](https://discourse.julialang.org/t/fields-of-an-anonymous-function/44562/2 "2020-08-08T11:59:51Z")

</div>

If I remember correctly his is an implementation detail of anonymous functions and thus intentially not documented.

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [August 8, 2020, 12:01pm UTC](https://discourse.julialang.org/t/fields-of-an-anonymous-function/44562/3 "2020-08-08T12:01:46Z")

</div>

Strange then that Flux.jl features that behavior in their intro examples: [Basics · Flux](https://fluxml.ai/Flux.jl/stable/models/basics/#Building-Layers-1)

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [August 8, 2020, 12:19pm UTC](https://discourse.julialang.org/t/fields-of-an-anonymous-function/44562/4 "2020-08-08T12:19:24Z")

</div>

I cannot comment on that, but here is some other reference:

> [@Closures as "classic object-oriented programming"](https://discourse.julialang.org/t/closures-as-classic-object-oriented-programming/33434/2):
>
> It uses an implementation detail about the representation of closures, namely [https://docs.julialang.org/en/v1.5-dev/devdocs/functions/#Closures-1](https://docs.julialang.org/en/v1.5-dev/devdocs/functions/#Closures-1) But I think this is an implementation detail, and not part of the official API, so I would not rely on this.

> [@Writing complex Flux Models](https://discourse.julialang.org/t/writing-complex-flux-models/40439/4):
>
> I think I’ve seen somewhere that closures being anonymous structs is an implementation detail which it is not recommended to depend on. Of course, if it is a single purpose project then by all means, just go ahead and implement that functor in there and use it while it works. # Too lazy to load Flux, I think something like this might work except closurefunctor shall be Flux.functor julia\> function test(a, b) return function(x) return a .+ b .\* x end end test (gene…

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [August 8, 2020, 12:23pm UTC](https://discourse.julialang.org/t/fields-of-an-anonymous-function/44562/5 "2020-08-08T12:23:33Z")

</div>

> [@Closures as "classic object-oriented programming"](https://discourse.julialang.org/t/closures-as-classic-object-oriented-programming/33434/2):
>
> [Julia Functions · The Julia Language](https://docs.julialang.org/en/v1.5-dev/devdocs/functions/#Closures-1)

that’s the part of the documentation I didn’t read 😛

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 8, 2020, 12:33pm UTC](https://discourse.julialang.org/t/fields-of-an-anonymous-function/44562/6 "2020-08-08T12:33:28Z")

</div>

That’s the _developer_ docs. Useful if you want to contribute to Julia or understand the internals, but relying on the latter can break your code without warnings.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [August 8, 2020, 12:52pm UTC](https://discourse.julialang.org/t/fields-of-an-anonymous-function/44562/7 "2020-08-08T12:52:25Z")

</div>

Hm, I don’t see it in the link; they use that with `Affine` but that’s a struct, not an anonymous function. Did I just miss it?

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [August 8, 2020, 12:54pm UTC](https://discourse.julialang.org/t/fields-of-an-anonymous-function/44562/8 "2020-08-08T12:54:23Z")

</div>

In the second code block the following line appears, where `linear1` is an anonymous function.

```julia
linear1 = linear(5, 3) # we can access linear1.W etc

```

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [August 8, 2020, 1:19pm UTC](https://discourse.julialang.org/t/fields-of-an-anonymous-function/44562/9 "2020-08-08T13:19:30Z")

</div>

Maybe I should not have used the wording “features that behavior” for an inline comment 🤔
