# Function factories or callable structs?

**URL:** <https://discourse.julialang.org/t/function-factories-or-callable-structs/52987>\
**Category:** General Usage\
**Tags:** parametric-types, function\
**Created:** [January 7, 2021, 1:21pm UTC](https://discourse.julialang.org/t/function-factories-or-callable-structs/52987 "2021-01-07T13:21:15Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Post date:** [January 7, 2021, 1:21pm UTC](https://discourse.julialang.org/t/function-factories-or-callable-structs/52987/1 "2021-01-07T13:21:15Z")

</div>

I want to create parametric functions and see two ways of doing that:

**Function factory method**

```julia-repl
julia> function createAdder(a)
           function adder(b)
               a+b
           end
       end

julia> plus4 = createAdder(4)
julia> plus4(5)
9

```

**Struct based method**

```julia-repl
julia> struct Adder{T}
            a::T
       end
julia> function (f::Adder)(b)
           f.a + b
       end
julia> plus4 = Adder(4)
julia> plus4(5)
9

```

Is there any advantage of one approach over the other in terms of performance and things one has to be careful about to avoid runtime dispatching?

---

<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:** [January 7, 2021, 1:39pm UTC](https://discourse.julialang.org/t/function-factories-or-callable-structs/52987/2 "2021-01-07T13:39:20Z")

</div>

I prefer the `struct`-based strategy because:

1. ~~For each type `T` you use there will be one type of `Adder{T}`. The function factory strategy will create different closures (each with a different type) each time you run it, even with the same value. It seems to me, therefore, that the function factory strategy overspecializes.~~ I was wrong about this, see @rdeits answer below. If the closures come all from the same factory (i.e., outer function) they will have the same type for the same type of captured variables (if the compiler infer the types right, see the second point). So, for the examples above it would be the same. The `struct` approach can give you a finer control over specialization, as you can decide how much you want to parametrize the `struct` type over the captured variables, and you know the type is the same being built inside any function.
2. There are some performance problems with closures, while I am not sure if they would affect your specific case: [https://github.com/JuliaLang/julia/issues/15276#issuecomment-628844489](https://github.com/JuliaLang/julia/issues/15276#issuecomment-628844489) Basically, closures often lose type information of captured bindings and end up boxing them unnecessarily.
3. You can use a `mutable struct` to change the boxed value if you need it, and if you do not then you can use an immutable `struct` which (I think) it is ~~more lightweight than a closure~~ EDIT: seems like they are about the same, as [closures lower to anonymous immutable structs](https://github.com/JuliaLang/julia/pull/31253).

---

<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:** [January 7, 2021, 2:56pm UTC](https://discourse.julialang.org/t/function-factories-or-callable-structs/52987/3 "2021-01-07T14:56:50Z")

</div>

> [@Henrique\_Becker](#):
>
> For each type `T` you use there will be one type of `Adder{T}` . The function factory strategy will create different closures (each with a different type) each time you run it, even with the same value. It seems to me, therefore, that the function factory strategy overspecializes.

This is not actually true, and it’s easy to verify that with the example from the OP:

```julia
julia> function createAdder(a)
           function adder(b)
               a+b
           end
       end
createAdder (generic function with 1 method)

julia> plus4 = createAdder(4)
(::var"#adder#5"{Int64}) (generic function with 1 method)

julia> plus5 = createAdder(5)
(::var"#adder#5"{Int64}) (generic function with 1 method)

julia> typeof(plus4) == typeof(plus5)
true

```

The closure approach creates a new type only when you define the `createAdder` function, so the number of types created is exactly the same as the struct approach.

---

<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:** [January 7, 2021, 4:07pm UTC](https://discourse.julialang.org/t/function-factories-or-callable-structs/52987/4 "2021-01-07T16:07:47Z")

</div>

Good catch, I apologize for my mistake, I tested this before pointing out but my test was badly designed:

```julia
julia> typeof((x -> (y -> x + y))(5))
var"#2#4"{Int64}

julia> typeof((x -> (y -> x + y))(5))
var"#6#8"{Int64}

```

I only cared for the value passed to the function, but not the fact I was using an anonymous outer function.

---

<div class="post-metadata">

**Author:** ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Post date:** [January 7, 2021, 8:04pm UTC](https://discourse.julialang.org/t/function-factories-or-callable-structs/52987/5 "2021-01-07T20:04:15Z")

</div>

I also benchmarked both approaches for my problem in question now and and the differences are negligible. So as far as I can tell it is up to personal taste. I will stick with the struct-based approach for now

---

<div class="post-metadata">

**Author:** ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)\
**Post date:** [January 7, 2021, 8:19pm UTC](https://discourse.julialang.org/t/function-factories-or-callable-structs/52987/6 "2021-01-07T20:19:37Z")

</div>

To add to rdeits excellent response: If I understand correctly, closures in julia **are implemented** with the `struct` approach you have described above (there are other possible implementations). So expect no fundamental difference…

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 7, 2021, 8:24pm UTC](https://discourse.julialang.org/t/function-factories-or-callable-structs/52987/7 "2021-01-07T20:24:37Z")

</div>

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:** ![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:** [January 7, 2021, 8:27pm UTC](https://discourse.julialang.org/t/function-factories-or-callable-structs/52987/8 "2021-01-07T20:27:37Z")

</div>

The [performance problem with closures](https://github.com/JuliaLang/julia/issues/15276#issuecomment-628844489) are not something that can be avoided using the “explicit” `struct` approach? In other words, sometimes using `struct`s explicitly the programmer cannot end being smarter than the current closure lowering approach?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 7, 2021, 8:30pm UTC](https://discourse.julialang.org/t/function-factories-or-callable-structs/52987/9 "2021-01-07T20:30:06Z")

</div>

Yes, in those cases a typed mutable struct may end up being more efficient because as I understand it the compiler is not that great yet at limiting the type of “boxed” variables in the closure object.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 8, 2021, 12:11am UTC](https://discourse.julialang.org/t/function-factories-or-callable-structs/52987/10 "2021-01-08T00:11:59Z")

</div>

Another advantage of callable structs is that you can do more things with them than call them. e.g. a custom `show` method for pretty printing.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 8, 2021, 12:16am UTC](https://discourse.julialang.org/t/function-factories-or-callable-structs/52987/11 "2021-01-08T00:16:33Z")

</div>

They’re also just more legible even without custom `show` methods.

```julia
julia> createAdder(4)
(::var"#adder#8"{Int64}) (generic function with 1 method)

julia> Adder(4)
Adder{Int64}(4)

```
