# \#closure

**URL:** https://discourse.julialang.org/tag/closure/864.md?page=1

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

**Page:** 2

---

## [Are "Closures should be avoided whenever possible" still valid in Julia v1.9+?](https://discourse.julialang.org/t/are-closures-should-be-avoided-whenever-possible-still-valid-in-julia-v1-9/95893)

<div class="topic-metadata">

**Author:** [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Replies:** 5\
**Last updated:** [March 12, 2023, 12:32am UTC](https://discourse.julialang.org/t/are-closures-should-be-avoided-whenever-possible-still-valid-in-julia-v1-9/95893 "2023-03-12T00:32:39Z")

</div>

I have posted the same question on Are the examples in "Closures should be avoided whenever possible" still valid in Julia v1.9+? · Issue #34 · SciML/SciMLStyle · GitHub. I just tested the examples in the section “Closu…

---

## [Call FastClosures.jl \`@closure\` in Array Comprehensions?](https://discourse.julialang.org/t/call-fastclosures-jl-closure-in-array-comprehensions/93622)

<div class="topic-metadata">

**Author:** [@uniment](https://discourse.julialang.org/u/uniment)\
**Replies:** 2\
**Last updated:** [February 3, 2023, 1:44am UTC](https://discourse.julialang.org/t/call-fastclosures-jl-closure-in-array-comprehensions/93622 "2023-02-03T01:44:42Z")

</div>

Like with any closure, array comprehensions present a performance challenge if they capture a variable whose identifier is reassigned: julia\> using BenchmarkTools julia\> const a=\[1:1000;\]; julia\> @btime let b=a; \[b\[x\]…

---

## [Avoiding Allocations with User provided Closures](https://discourse.julialang.org/t/avoiding-allocations-with-user-provided-closures/31279)

<div class="topic-metadata">

**Author:** [@cshenton](https://discourse.julialang.org/u/cshenton)\
**Replies:** 12\
**Last updated:** [February 2, 2023, 3:29am UTC](https://discourse.julialang.org/t/avoiding-allocations-with-user-provided-closures/31279 "2023-02-02T03:29:41Z")

</div>

I’m writing a query API that allows a user to pass in a closure to be run on every entry satisfying the query, however this usually results in undesirable boxing. For example, here’s the kind of API I want to offer, with…

---

## ["inline" variable in closure](https://discourse.julialang.org/t/inline-variable-in-closure/93213)

<div class="topic-metadata">

**Author:** [@OvidiusCicero](https://discourse.julialang.org/u/OvidiusCicero)\
**Replies:** 2\
**Last updated:** [January 19, 2023, 2:22pm UTC](https://discourse.julialang.org/t/inline-variable-in-closure/93213 "2023-01-19T14:22:25Z")

</div>

I have a some function depending on an independent variable and some general parameters. In a simplified version f(a, x) = a + x where a is some complicated struct of parameters which I ideally would like to type only …

---

## [Capture \`DataType\` in closure](https://discourse.julialang.org/t/capture-datatype-in-closure/92752)

<div class="topic-metadata">

**Author:** [@hexaeder](https://discourse.julialang.org/u/hexaeder)\
**Replies:** 7\
**Last updated:** [January 11, 2023, 9:41am UTC](https://discourse.julialang.org/t/capture-datatype-in-closure/92752 "2023-01-11T09:41:04Z")

</div>

Why is it apparently not possible to capture a DataType inside a closure in a typestable manner? Consider function create\_closure1(data) bufT = eltype(data) (i) -\> begin buf = Vector{bufT}(undef,1) …

---

## [\`Core.Box\` around captured variable](https://discourse.julialang.org/t/core-box-around-captured-variable/91299)

<div class="topic-metadata">

**Author:** [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Replies:** 2\
**Last updated:** [December 6, 2022, 1:24pm UTC](https://discourse.julialang.org/t/core-box-around-captured-variable/91299 "2022-12-06T13:24:33Z")

</div>

If I define a variable inside an outer function that generates a closure, this variable gets boxed inside the closer. Does this affect performance and if yes, how do I best avoid it? Here is a simple MWE: julia\> functi…

---

## [Question about functors and closures (I think 😅), with an example, but really a general Q](https://discourse.julialang.org/t/question-about-functors-and-closures-i-think-with-an-example-but-really-a-general-q/88758)

<div class="topic-metadata">

**Author:** [@fins](https://discourse.julialang.org/u/fins)\
**Replies:** 27\
**Last updated:** [October 18, 2022, 7:54pm UTC](https://discourse.julialang.org/t/question-about-functors-and-closures-i-think-with-an-example-but-really-a-general-q/88758 "2022-10-18T19:54:48Z")

</div>

Hi, I’ve just been fiddling with some bits of code and am still a bit blown away by some julian’ language-constructs. I’ve been doing oo-programming, mainly, for a considerable timespan & my (purely ever academic) expos…

---

## [Immediately executing closure as function barrier](https://discourse.julialang.org/t/immediately-executing-closure-as-function-barrier/87976)

<div class="topic-metadata">

**Author:** [@torrance](https://discourse.julialang.org/u/torrance)\
**Replies:** 16\
**Last updated:** [September 30, 2022, 10:08pm UTC](https://discourse.julialang.org/t/immediately-executing-closure-as-function-barrier/87976 "2022-09-30T22:08:50Z")

</div>

It’s common for code to do a bunch of allocations up front, that may or may not be type stable, and then call out to another function to do some actual work with those allocations. This ‘function barrier’ enables the ker…

---

## [Type issue with captured variables -\> let workaround failed](https://discourse.julialang.org/t/type-issue-with-captured-variables-let-workaround-failed/85661)

<div class="topic-metadata">

**Author:** [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Replies:** 3\
**Last updated:** [August 13, 2022, 6:59am UTC](https://discourse.julialang.org/t/type-issue-with-captured-variables-let-workaround-failed/85661 "2022-08-13T06:59:30Z")

</div>

Hi! I’m aware of #15276 but today I encountered an issue in the proposed workaround. using FFTW function f(x) p = plan\_fft(x) x\_ft = p \* x # given workaround in performance tips g = let p = p …

---

## [Closures over types are type unstable](https://discourse.julialang.org/t/closures-over-types-are-type-unstable/82337)

<div class="topic-metadata">

**Author:** [@tictaccat](https://discourse.julialang.org/u/tictaccat)\
**Replies:** 6\
**Last updated:** [June 6, 2022, 7:33pm UTC](https://discourse.julialang.org/t/closures-over-types-are-type-unstable/82337 "2022-06-06T19:33:00Z")

</div>

The closure returned by the following function function f(x::AbstractArray) T = eltype(x) ax = axes(x) return () -\> zeros(T, 1000) end is type unstable: @code\_warntype f(rand(3))() Met…

---

## [Why is closure slower than handmade callable struct?](https://discourse.julialang.org/t/why-is-closure-slower-than-handmade-callable-struct/80361)

<div class="topic-metadata">

**Author:** [@FedericoStra](https://discourse.julialang.org/u/FedericoStra)\
**Replies:** 4\
**Last updated:** [May 2, 2022, 1:49pm UTC](https://discourse.julialang.org/t/why-is-closure-slower-than-handmade-callable-struct/80361 "2022-05-02T13:49:38Z")

</div>

I’m having a hard time trying to understand why in the following code a closure is slower than an equivalent hand-made callable struct. using BenchmarkTools using Interpolations using LinearAlgebra: norm using StaticArr…

---

## [How to capture an index in closure without allocating (or workaround)?](https://discourse.julialang.org/t/how-to-capture-an-index-in-closure-without-allocating-or-workaround/76334)

<div class="topic-metadata">

**Author:** [@taotree](https://discourse.julialang.org/u/taotree)\
**Replies:** 4\
**Last updated:** [February 14, 2022, 9:20pm UTC](https://discourse.julialang.org/t/how-to-capture-an-index-in-closure-without-allocating-or-workaround/76334 "2022-02-14T21:20:36Z")

</div>

Sorry the code is a bit long but the middle testIt! function is where the question is. If I run run() as-is, there are ~160k allocations. However, if I comment out the code with i, then there are no allocations. I’m assu…

---

## [Serializing Functions / Methods / Closures](https://discourse.julialang.org/t/serializing-functions-methods-closures/57061)

<div class="topic-metadata">

**Author:** [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Replies:** 3\
**Last updated:** [December 1, 2021, 8:14am UTC](https://discourse.julialang.org/t/serializing-functions-methods-closures/57061 "2021-12-01T08:14:35Z")

</div>

Hi all, over the last months I’ve spent quite some time to bring JLD2 back up to speed. One of the remaining pain points is that it is not possible to properly store and recover (anonymous) functions and closures. This…

---

## [Closures: pass by value or reference?](https://discourse.julialang.org/t/closures-pass-by-value-or-reference/71155)

<div class="topic-metadata">

**Author:** [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Replies:** 9\
**Last updated:** [November 8, 2021, 7:13pm UTC](https://discourse.julialang.org/t/closures-pass-by-value-or-reference/71155 "2021-11-08T19:13:49Z")

</div>

Is the following true? “When copied inside a function, variables become local and functions become closures.” (unknown) What causes then the different behavior below? solver(f,x) = f(x) a, b, c = 1, 2, 3; g(x) = a\*x…

---

## [Function closure with large arrays/matrices scaling horribly](https://discourse.julialang.org/t/function-closure-with-large-arrays-matrices-scaling-horribly/71027)

<div class="topic-metadata">

**Author:** [@vivian-rogers](https://discourse.julialang.org/u/vivian-rogers)\
**Replies:** 6\
**Last updated:** [November 6, 2021, 3:19pm UTC](https://discourse.julialang.org/t/function-closure-with-large-arrays-matrices-scaling-horribly/71027 "2021-11-06T15:19:33Z")

</div>

Hi! So I am trying to set up a useful function f to pass around the rest of my code as follows: function f\_prepostproc(params) # do stuff f = f\_generator(params) # evaluate f at certain x values, save to file…

---

## [Using a closure with mutable captured values inside an @distributed loop](https://discourse.julialang.org/t/using-a-closure-with-mutable-captured-values-inside-an-distributed-loop/68818)

<div class="topic-metadata">

**Author:** [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Replies:** 2\
**Last updated:** [September 27, 2021, 9:02pm UTC](https://discourse.julialang.org/t/using-a-closure-with-mutable-captured-values-inside-an-distributed-loop/68818 "2021-09-27T21:02:59Z")

</div>

The Julia manual says the following about the @distributed macro: Any variables used inside the parallel loop will be copied and broadcast to each process. My question is: Does this also apply to functions and closur…

---

## [Using closures for performance gain when handling datasets and making repeated function calls](https://discourse.julialang.org/t/using-closures-for-performance-gain-when-handling-datasets-and-making-repeated-function-calls/66809)

<div class="topic-metadata">

**Author:** [@rubaiyat](https://discourse.julialang.org/u/rubaiyat)\
**Replies:** 3\
**Last updated:** [August 24, 2021, 9:47am UTC](https://discourse.julialang.org/t/using-closures-for-performance-gain-when-handling-datasets-and-making-repeated-function-calls/66809 "2021-08-24T09:47:54Z")

</div>

Hello everyone, I’m in a situation where I’m estimating parameters of a model using a dataset. This requires optimization, and my question is about how I can leverage closures to speed up the process. I’d be curious to…

---

## [Allocations when passing closure as parameter](https://discourse.julialang.org/t/allocations-when-passing-closure-as-parameter/63082)

<div class="topic-metadata">

**Author:** [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Replies:** 2\
**Last updated:** [June 17, 2021, 1:13pm UTC](https://discourse.julialang.org/t/allocations-when-passing-closure-as-parameter/63082 "2021-06-17T13:13:04Z")

</div>

I struggling to obtain a minimal working example. But I have one situation which looks like: function outer!(f::Function, output,x::AbstractVector) for i in 1:10 output = inner!(f,output,x) end return output …

---

## [Tutorial on closures?](https://discourse.julialang.org/t/tutorial-on-closures/62749)

<div class="topic-metadata">

**Author:** [@Arrigo\_Benedetti](https://discourse.julialang.org/u/Arrigo_Benedetti)\
**Replies:** 5\
**Last updated:** [June 12, 2021, 9:07pm UTC](https://discourse.julialang.org/t/tutorial-on-closures/62749 "2021-06-12T21:07:59Z")

</div>

I want to learn more about closures and their use with Julia. Does anyone recommend any paper/tutorial/book/blog post clearly explaining closures and what are their use cases for Julia programming? Most of the material …

---

## [Closure in struct type inference](https://discourse.julialang.org/t/closure-in-struct-type-inference/62180)

<div class="topic-metadata">

**Author:** [@cdawg](https://discourse.julialang.org/u/cdawg)\
**Replies:** 12\
**Last updated:** [June 3, 2021, 1:57pm UTC](https://discourse.julialang.org/t/closure-in-struct-type-inference/62180 "2021-06-03T13:57:10Z")

</div>

This comes up a lot in my code style. I’ve been doing this thing which is to store methods inside a struct as closures. (sort of like oo programming I guess?). This is a lot more convenient than storing the captured valu…

---

## [Allocations with closure and mul!](https://discourse.julialang.org/t/allocations-with-closure-and-mul/60744)

<div class="topic-metadata">

**Author:** [@geoffroyleconte](https://discourse.julialang.org/u/geoffroyleconte)\
**Replies:** 4\
**Last updated:** [May 10, 2021, 2:19pm UTC](https://discourse.julialang.org/t/allocations-with-closure-and-mul/60744 "2021-05-10T14:19:44Z")

</div>

Hi! I have an issue with allocations using the mul! function and a closure: using LinearAlgebra, Test struct operator{T} prod! end function operator(A :: AbstractArray{T}) where T prod! = (res, u, α, β) -\> m…

---

## [Binding variable from function which isn't defined in a closure to a closed variable](https://discourse.julialang.org/t/binding-variable-from-function-which-isnt-defined-in-a-closure-to-a-closed-variable/60703)

<div class="topic-metadata">

**Author:** [@jt1](https://discourse.julialang.org/u/jt1)\
**Replies:** 2\
**Last updated:** [May 7, 2021, 11:52am UTC](https://discourse.julialang.org/t/binding-variable-from-function-which-isnt-defined-in-a-closure-to-a-closed-variable/60703 "2021-05-07T11:52:24Z")

</div>

Hi, the goal is to get a shared variable between functions by defining a closure. The following minimal working example works as intended: disp, change = let x = \[1,2\] disp = () -\> println(x) change = function(…

---

## [Performance closures with changing value of closed over variables](https://discourse.julialang.org/t/performance-closures-with-changing-value-of-closed-over-variables/59671)

<div class="topic-metadata">

**Author:** [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Replies:** 1\
**Last updated:** [April 20, 2021, 4:58pm UTC](https://discourse.julialang.org/t/performance-closures-with-changing-value-of-closed-over-variables/59671 "2021-04-20T16:58:36Z")

</div>

Similar things have been discussed in other threads, but this particular example was surprising to me: julia\> function main(x) g(x,y) = x + y f(x,inner\_g) = inner\_g(x) y = 1 f(x, x -\> …

---

## [How to avoid boxed function names in closures?](https://discourse.julialang.org/t/how-to-avoid-boxed-function-names-in-closures/58579)

<div class="topic-metadata">

**Author:** [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Replies:** 6\
**Last updated:** [April 5, 2021, 8:29pm UTC](https://discourse.julialang.org/t/how-to-avoid-boxed-function-names-in-closures/58579 "2021-04-05T20:29:18Z")

</div>

Hello! In order to avoid performance problems, I would like to know whether boxes can be avoided in two kinds of closures. Without closures, all problems mentioned below disappear. I’m using Julia 1.6.0. Keyword argumen…

---

## [A question about closures](https://discourse.julialang.org/t/a-question-about-closures/55325)

<div class="topic-metadata">

**Author:** [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Replies:** 6\
**Last updated:** [February 15, 2021, 5:06pm UTC](https://discourse.julialang.org/t/a-question-about-closures/55325 "2021-02-15T17:06:41Z")

</div>

Hi, I’m trying to work out how to construct a closure for the following case: The Agents package has a random\_agent(agent, condition) function and I want to call it with a condition closure. I though the following woul…

---

## [Accessing data in a closure?](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930)

<div class="topic-metadata">

**Author:** [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Replies:** 12\
**Last updated:** [February 9, 2021, 8:38pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-closure/54930 "2021-02-09T20:38:27Z")

</div>

Learning to work with closures. This is a construct to find out what is possible. 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…

---

## [Is there a way to modify captured variables in a closure?](https://discourse.julialang.org/t/is-there-a-way-to-modify-captured-variables-in-a-closure/31213)

<div class="topic-metadata">

**Author:** [@marius311](https://discourse.julialang.org/u/marius311)\
**Replies:** 15\
**Last updated:** [January 8, 2021, 9:05am UTC](https://discourse.julialang.org/t/is-there-a-way-to-modify-captured-variables-in-a-closure/31213 "2021-01-08T09:05:59Z")

</div>

Suppose I have some closure like, f = let x = 2 () -\> x^2 end Is there some way to create a new closure from this where I modify the type and value of x? Something like, g = deepcopy(f) g.x = "2" g() # would give …

---

## [Again on closures and type instability](https://discourse.julialang.org/t/again-on-closures-and-type-instability/51664)

<div class="topic-metadata">

**Author:** [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Replies:** 6\
**Last updated:** [December 11, 2020, 6:10pm UTC](https://discourse.julialang.org/t/again-on-closures-and-type-instability/51664 "2020-12-11T18:10:58Z")

</div>

Consider the simple functions: inner(x,a) = a\*x outer(f,x) = f(x) If one calls outer using a closure and global, non-constant parameter a , we have a type-instability: julia\> a = 5; x = 2; julia\> @code\_warntype outer…

---

## [Closures and Let Blocks](https://discourse.julialang.org/t/closures-and-let-blocks/50197)

<div class="topic-metadata">

**Author:** [@nucklass](https://discourse.julialang.org/u/nucklass)\
**Replies:** 4\
**Last updated:** [November 17, 2020, 6:48am UTC](https://discourse.julialang.org/t/closures-and-let-blocks/50197 "2020-11-17T06:48:07Z")

</div>

Anyone have any basic examples of when to use/what the effects are of having a let block along with a closure? I’ve been looking to learn more about functional programming in Julia and reading through the manual, but I …

---

## [How is equality of anonymous function closures checked?](https://discourse.julialang.org/t/how-is-equality-of-anonymous-function-closures-checked/49397)

<div class="topic-metadata">

**Author:** [@mahrud](https://discourse.julialang.org/u/mahrud)\
**Replies:** 14\
**Last updated:** [November 5, 2020, 2:13pm UTC](https://discourse.julialang.org/t/how-is-equality-of-anonymous-function-closures-checked/49397 "2020-11-05T14:13:08Z")

</div>

I was wondering how equality of functions works in Julia, and eventually stumbled on this example, which I can’t quite explain: julia\> f = x -\> y -\> x + y #1 (generic function with 1 method) julia\> g = f(1); h = f(1); …

[Previous page](https://discourse.julialang.org/tag/closure/864.md?match_all_tags=true&tags%5B%5D=closure)

[Next page](https://discourse.julialang.org/tag/closure/864.md?match_all_tags=true&page=2&tags%5B%5D=closure)
