# \#closure

**URL:** https://discourse.julialang.org/tag/closure/864.md

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

---

## [Help with IR Manipulation](https://discourse.julialang.org/t/help-with-ir-manipulation/135724)

<div class="topic-metadata">

**Author:** [@charlesknipp](https://discourse.julialang.org/u/charlesknipp)\
**Replies:** 0\
**Last updated:** [February 18, 2026, 4:31pm UTC](https://discourse.julialang.org/t/help-with-ir-manipulation/135724 "2026-02-18T16:31:55Z")

</div>

Since this is a relatively complex stack, I will begin with an overarching explanation of my problem, then contextualize it further. Keeping it Vague Suppose I have a misty closure (MistyClosures.jl), where I want to sp…

---

## [Closures magic and how stable?](https://discourse.julialang.org/t/closures-magic-and-how-stable/135408)

<div class="topic-metadata">

**Author:** [@mattsignorelli](https://discourse.julialang.org/u/mattsignorelli)\
**Replies:** 7\
**Last updated:** [February 2, 2026, 4:53pm UTC](https://discourse.julialang.org/t/closures-magic-and-how-stable/135408 "2026-02-02T16:53:47Z")

</div>

There is a feature in Julia that has enabled us to do some magical things. It is all thanks to this: julia\> adder(f, a) = ()-\> f() + a adder (generic function with 1 method) julia\> f = ()-\>5 #3 (generic function with 1…

---

## [Type instability seemingly due to redundant inner functions that are never called](https://discourse.julialang.org/t/type-instability-seemingly-due-to-redundant-inner-functions-that-are-never-called/133227)

<div class="topic-metadata">

**Author:** [@Nikos\_Gianniotis](https://discourse.julialang.org/u/Nikos_Gianniotis)\
**Replies:** 5\
**Last updated:** [October 18, 2025, 5:27pm UTC](https://discourse.julialang.org/t/type-instability-seemingly-due-to-redundant-inner-functions-that-are-never-called/133227 "2025-10-18T17:27:26Z")

</div>

I have created an MWE that can readily executed by copy-pasting to the REPL, provided package DispatchDoctor.jl is installed. What follows below is: A variable Z is created in global scope (see end of script below). …

---

## [Behaviors of \`deepcopy\` for a function constructed with different ways of referencing a global variable](https://discourse.julialang.org/t/behaviors-of-deepcopy-for-a-function-constructed-with-different-ways-of-referencing-a-global-variable/128939)

<div class="topic-metadata">

**Author:** [@frankwswang](https://discourse.julialang.org/u/frankwswang)\
**Replies:** 2\
**Last updated:** [May 13, 2025, 1:30am UTC](https://discourse.julialang.org/t/behaviors-of-deepcopy-for-a-function-constructed-with-different-ways-of-referencing-a-global-variable/128939 "2025-05-13T01:30:07Z")

</div>

This discovery stemmed from the reply in another post, but since it’s less relevant to the initial post, I thought I would make a new post dedicated to it. julia\> box = fill(1) 0-dimensional Array{Int64, 0}: 1 julia\> f…

---

## [Difference between vector of functions typed explicitly or via list comprehension](https://discourse.julialang.org/t/difference-between-vector-of-functions-typed-explicitly-or-via-list-comprehension/128671)

<div class="topic-metadata">

**Author:** [@hawahe](https://discourse.julialang.org/u/hawahe)\
**Replies:** 4\
**Last updated:** [May 4, 2025, 10:16am UTC](https://discourse.julialang.org/t/difference-between-vector-of-functions-typed-explicitly-or-via-list-comprehension/128671 "2025-05-04T10:16:51Z")

</div>

Are the following two ways to make a vector of functions equivalent? The latter gives both anonymous functions the name #1 when I expected to see #1 and #2. julia\> Function\[(x) -\> x^2, (x) -\> x^3\] 2-element Vector{Funct…

---

## [Anonymous Functions and Overwriting Arguments](https://discourse.julialang.org/t/anonymous-functions-and-overwriting-arguments/127586)

<div class="topic-metadata">

**Author:** [@phK3](https://discourse.julialang.org/u/phK3)\
**Replies:** 7\
**Last updated:** [April 3, 2025, 4:41am UTC](https://discourse.julialang.org/t/anonymous-functions-and-overwriting-arguments/127586 "2025-04-03T04:41:38Z")

</div>

I’ve stumbled upon the following (in my mind) weird behavior: If you define a named function that returns a function f1 based on a parameter a, the returned function stays the same, even if a gets assigned another value…

---

## [Recursive closures/inner functions -- how to avoid boxing?](https://discourse.julialang.org/t/recursive-closures-inner-functions-how-to-avoid-boxing/124758)

<div class="topic-metadata">

**Author:** [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Replies:** 6\
**Last updated:** [January 14, 2025, 1:40pm UTC](https://discourse.julialang.org/t/recursive-closures-inner-functions-how-to-avoid-boxing/124758 "2025-01-14T13:40:28Z")

</div>

Consider code like julia\> function outer() function inner() false && inner() end inner end julia\> typeof(outer()).types svec(Core.Box) So this kind of code creates a box for the loca…

---

## [Closure with let block: Simpler syntax for capturing variables?](https://discourse.julialang.org/t/closure-with-let-block-simpler-syntax-for-capturing-variables/124740)

<div class="topic-metadata">

**Author:** [@linwaytin](https://discourse.julialang.org/u/linwaytin)\
**Replies:** 4\
**Last updated:** [January 14, 2025, 12:46am UTC](https://discourse.julialang.org/t/closure-with-let-block-simpler-syntax-for-capturing-variables/124740 "2025-01-14T00:46:40Z")

</div>

The example is taken from the post "A Tragedy of Julia’s Type System" - #30 by foobar\_lv2 function f() x = 0 if false x=1 end g = let x=x () -\> x end end This form is suggested in the manual, and inde…

---

## [Getting rid of ForwardDiff.jacobian! allocations when using closures](https://discourse.julialang.org/t/getting-rid-of-forwarddiff-jacobian-allocations-when-using-closures/123939)

<div class="topic-metadata">

**Author:** [@franckgaga](https://discourse.julialang.org/u/franckgaga)\
**Replies:** 6\
**Last updated:** [December 19, 2024, 7:30pm UTC](https://discourse.julialang.org/t/getting-rid-of-forwarddiff-jacobian-allocations-when-using-closures/123939 "2024-12-19T19:30:38Z")

</div>

I’m trying to get rid of the memory allocations in jacobian!. For this, I need to pass a cfg argument with a pre-allocated JacobianConfig to the function. According to the doc, its constructor expects to receive the func…

---

## [Type stability in closures](https://discourse.julialang.org/t/type-stability-in-closures/123227)

<div class="topic-metadata">

**Author:** [@AntonReinhard](https://discourse.julialang.org/u/AntonReinhard)\
**Replies:** 13\
**Last updated:** [November 28, 2024, 11:00pm UTC](https://discourse.julialang.org/t/type-stability-in-closures/123227 "2024-11-28T23:00:13Z")

</div>

I’m doing a bunch of code generation and came across a strange behaviour involving type stability, closures and variable captures. I reduced the problem to the following example: function f(a, b) \_f = (x, y) -\> begi…

---

## [Closures and type inference](https://discourse.julialang.org/t/closures-and-type-inference/123026)

<div class="topic-metadata">

**Author:** [@miguelborrero](https://discourse.julialang.org/u/miguelborrero)\
**Replies:** 10\
**Last updated:** [November 26, 2024, 3:44am UTC](https://discourse.julialang.org/t/closures-and-type-inference/123026 "2024-11-26T03:44:51Z")

</div>

Hi there, I am reading through some Julia learning material and I had a question that someone here might be able to help with. The example given reads: function add\_n\_fn(n) function \_add\_n(x) return x+n e…

---

## [Is it possible to avoid these excess allocations when using @batch or @threads?](https://discourse.julialang.org/t/is-it-possible-to-avoid-these-excess-allocations-when-using-batch-or-threads/122208)

<div class="topic-metadata">

**Author:** [@Tetrakai](https://discourse.julialang.org/u/Tetrakai)\
**Replies:** 4\
**Last updated:** [November 8, 2024, 10:37am UTC](https://discourse.julialang.org/t/is-it-possible-to-avoid-these-excess-allocations-when-using-batch-or-threads/122208 "2024-11-08T10:37:37Z")

</div>

It took some effort to come up with a short MWE for this issue. The goal is to avoid a few small allocations when mutating (an argument of an inner function) in parallel: MWEusing BenchmarkTools, Parameters, Polyester, …

---

## [Why are there no captured variables when the \`if\` expression is used as an rvalue?](https://discourse.julialang.org/t/why-are-there-no-captured-variables-when-the-if-expression-is-used-as-an-rvalue/121782)

<div class="topic-metadata">

**Author:** [@aaoo](https://discourse.julialang.org/u/aaoo)\
**Replies:** 5\
**Last updated:** [October 27, 2024, 8:08am UTC](https://discourse.julialang.org/t/why-are-there-no-captured-variables-when-the-if-expression-is-used-as-an-rvalue/121782 "2024-10-27T08:08:31Z")

</div>

When I was learning the Performance of captured variable, I found a second version of the example function that shows no captured variable in @code\_warntype. I am confused about the following three questions: Would the…

---

## [Benchmarking array closures](https://discourse.julialang.org/t/benchmarking-array-closures/75689)

<div class="topic-metadata">

**Author:** [@lewis](https://discourse.julialang.org/u/lewis)\
**Replies:** 3\
**Last updated:** [September 5, 2024, 6:24pm UTC](https://discourse.julialang.org/t/benchmarking-array-closures/75689 "2024-09-05T18:24:37Z")

</div>

Want to verify that I’ve done this in a way that gives valid results. In the code below dostuff! updates 2 arrays in place. clos! creates a closure that encloses the arrays and accepts arguments for the change factors. …

---

## [Trying to understand performance of closure from @code\_llvm](https://discourse.julialang.org/t/trying-to-understand-performance-of-closure-from-code-llvm/49361)

<div class="topic-metadata">

**Author:** [@tomohiro\_soejima](https://discourse.julialang.org/u/tomohiro_soejima)\
**Replies:** 2\
**Last updated:** [August 26, 2024, 8:17pm UTC](https://discourse.julialang.org/t/trying-to-understand-performance-of-closure-from-code-llvm/49361 "2024-08-26T20:17:55Z")

</div>

Hi, I am trying to better understand how closure works. I’ve inspected captured variable section of the manual, and tried inspecting the following functions. function abmult(r::Int) if r \< 0 r = -r end …

---

## [\[ANN\] CClosure.jl: C closures like what C does](https://discourse.julialang.org/t/ann-cclosure-jl-c-closures-like-what-c-does/81234)

<div class="topic-metadata">

**Author:** [@melonedo](https://discourse.julialang.org/u/melonedo)\
**Replies:** 0\
**Last updated:** [May 18, 2022, 6:16am UTC](https://discourse.julialang.org/t/ann-cclosure-jl-c-closures-like-what-c-does/81234 "2022-05-18T06:16:11Z")

</div>

Julia has a very powerful @cfunction macro that can make any pure Julia function callable to C. However, it only produces a single function pointer, contrary to modern C libraries where a pair of function pointer and a p…

---

## [Closure not shipping to remote workers except from Main?](https://discourse.julialang.org/t/closure-not-shipping-to-remote-workers-except-from-main/38831)

<div class="topic-metadata">

**Author:** [@marius311](https://discourse.julialang.org/u/marius311)\
**Replies:** 0\
**Last updated:** [May 5, 2020, 8:45pm UTC](https://discourse.julialang.org/t/closure-not-shipping-to-remote-workers-except-from-main/38831 "2020-05-05T20:45:39Z")

</div>

Consider the following code which generates a closure via an @eval (needed in my non-MWE), and then ships it to a remote worker to evaluate: module Foo using Distributed make\_closure(x) = @eval let x=$x; () -\> x; end …

---

## [Opaque closure and code\_typed](https://discourse.julialang.org/t/opaque-closure-and-code-typed/102690)

<div class="topic-metadata">

**Author:** [@willtebbutt](https://discourse.julialang.org/u/willtebbutt)\
**Replies:** 0\
**Last updated:** [August 10, 2023, 6:25pm UTC](https://discourse.julialang.org/t/opaque-closure-and-code-typed/102690 "2023-08-10T18:25:38Z")

</div>

I’m playing around with opaque closures. I’ve have managed to construct the following toy example: struct Foo x::Float64 end (x::Foo)(y) = cos(sin(getfield(x, 1) \* y)) @eval function make\_opaque\_closure(x) ret…

---

## [How to access the variable accessible by a closure?](https://discourse.julialang.org/t/how-to-access-the-variable-accessible-by-a-closure/118628)

<div class="topic-metadata">

**Author:** [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Replies:** 1\
**Last updated:** [August 26, 2024, 3:41pm UTC](https://discourse.julialang.org/t/how-to-access-the-variable-accessible-by-a-closure/118628 "2024-08-26T15:41:24Z")

</div>

in the following a is accessible by gun(): function fun() a = 2.0 function gun(b) return a + b end end g = fun() g(3.0) julia\> g = fun() (::var"#gun#10"{Float64}) (generic function with 1 method) …

---

## [Macro with variable argument list combined with closure](https://discourse.julialang.org/t/macro-with-variable-argument-list-combined-with-closure/117623)

<div class="topic-metadata">

**Author:** [@nietzsche](https://discourse.julialang.org/u/nietzsche)\
**Replies:** 2\
**Last updated:** [July 30, 2024, 11:19am UTC](https://discourse.julialang.org/t/macro-with-variable-argument-list-combined-with-closure/117623 "2024-07-30T11:19:06Z")

</div>

Hi, i have a question about variable arguments in macros together with the scope of them. Function count\_test() should be a closure over the variable count and the lambda should be generated with a macro. It should b…

---

## [Discontiguous Lexical Scope for Multiply Dispatched Functions](https://discourse.julialang.org/t/discontiguous-lexical-scope-for-multiply-dispatched-functions/114972)

<div class="topic-metadata">

**Author:** [@blabatt](https://discourse.julialang.org/u/blabatt)\
**Replies:** 9\
**Last updated:** [May 31, 2024, 3:31pm UTC](https://discourse.julialang.org/t/discontiguous-lexical-scope-for-multiply-dispatched-functions/114972 "2024-05-31T15:31:51Z")

</div>

I would like to create a closure that binds free variables with respect to a discontiguous “lexical environment” that encapsulates both the disparate method implementations and collective reference to a multiply-dispatch…

---

## [Can I use a closure, and if yes how?](https://discourse.julialang.org/t/can-i-use-a-closure-and-if-yes-how/113857)

<div class="topic-metadata">

**Author:** [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Replies:** 5\
**Last updated:** [May 5, 2024, 6:15pm UTC](https://discourse.julialang.org/t/can-i-use-a-closure-and-if-yes-how/113857 "2024-05-05T18:15:05Z")

</div>

I have the following code: using ControlPlots using KiteModels, KitePodModels, KiteUtils set = deepcopy(se()) kcu::KCU = KCU(set) kps4::KPS4 = KPS4(kcu) include("../examples/plot2d.jl") reltime = 0.0 integrator = Kit…

---

## [Help needed: Building an intuitive understanding of closures, scoping and type-stability](https://discourse.julialang.org/t/help-needed-building-an-intuitive-understanding-of-closures-scoping-and-type-stability/107960)

<div class="topic-metadata">

**Author:** [@Bizzi](https://discourse.julialang.org/u/Bizzi)\
**Replies:** 3\
**Last updated:** [December 26, 2023, 12:30am UTC](https://discourse.julialang.org/t/help-needed-building-an-intuitive-understanding-of-closures-scoping-and-type-stability/107960 "2023-12-26T00:30:20Z")

</div>

Even after quite some time, I still struggle with understanding how to create type-stable wrappers around simple functions. I’ve read the docs, I know that closures in julia use reference semantics instead of value seman…

---

## [Can someone explain closures to me](https://discourse.julialang.org/t/can-someone-explain-closures-to-me/105605)

<div class="topic-metadata">

**Author:** [@HashBrown](https://discourse.julialang.org/u/HashBrown)\
**Replies:** 13\
**Last updated:** [November 2, 2023, 8:21pm UTC](https://discourse.julialang.org/t/can-someone-explain-closures-to-me/105605 "2023-11-02T20:21:41Z")

</div>

Hi, I am familiar with what a closure is (a function that captures state outside its scope) but I am interested in how they work in Julia so that I can understand performance. In Performance Tips there is a section dedi…

---

## [Strange allocation in closure](https://discourse.julialang.org/t/strange-allocation-in-closure/104017)

<div class="topic-metadata">

**Author:** [@trahflow](https://discourse.julialang.org/u/trahflow)\
**Replies:** 3\
**Last updated:** [September 19, 2023, 2:41pm UTC](https://discourse.julialang.org/t/strange-allocation-in-closure/104017 "2023-09-19T14:41:18Z")

</div>

Hi, I observe an allocation involving a closure that I do not understand. Consider the following minimal working example: julia\> function outer(a) inner(b, c) = b, c a, inner end outer (ge…

---

## [How does a nested function display its local name only when assigned to a matching global name?](https://discourse.julialang.org/t/how-does-a-nested-function-display-its-local-name-only-when-assigned-to-a-matching-global-name/101685)

<div class="topic-metadata">

**Author:** [@Benny](https://discourse.julialang.org/u/Benny)\
**Replies:** 1\
**Last updated:** [July 17, 2023, 7:59am UTC](https://discourse.julialang.org/t/how-does-a-nested-function-display-its-local-name-only-when-assigned-to-a-matching-global-name/101685 "2023-07-17T07:59:14Z")

</div>

v1.8.3 MWE: julia\> function gh(n) g() = n h = () -\> n g, h end gh (generic function with 1 method) julia\> g2, h2 = gh(1) (var"#g#2"{Int64}(1), var"#1#3"{Int64}(1)) julia\> g, h = g2, h…

---

## [Type instability when using map with closures](https://discourse.julialang.org/t/type-instability-when-using-map-with-closures/101510)

<div class="topic-metadata">

**Author:** [@ynue](https://discourse.julialang.org/u/ynue)\
**Replies:** 3\
**Last updated:** [July 12, 2023, 9:54am UTC](https://discourse.julialang.org/t/type-instability-when-using-map-with-closures/101510 "2023-07-12T09:54:37Z")

</div>

I have observed a surprising behaviour of map with closures. It seems that type instabilities appear when the function called by map overwrites a variable that belongs to an outer scope (only changing its value while kee…

---

## [Inside closures, captured constructors are much slower than captured functions](https://discourse.julialang.org/t/inside-closures-captured-constructors-are-much-slower-than-captured-functions/99638)

<div class="topic-metadata">

**Author:** [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Replies:** 2\
**Last updated:** [May 31, 2023, 12:47pm UTC](https://discourse.julialang.org/t/inside-closures-captured-constructors-are-much-slower-than-captured-functions/99638 "2023-05-31T12:47:54Z")

</div>

I have noticed that inside function closures, using a captured variable with a type value as a constructor is much slower than using a variable with a function value. Consider the following (silly) example: function g(f…

---

## [Should closures be avoided?](https://discourse.julialang.org/t/should-closures-be-avoided/96073)

<div class="topic-metadata">

**Author:** [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Replies:** 15\
**Last updated:** [March 18, 2023, 10:52am UTC](https://discourse.julialang.org/t/should-closures-be-avoided/96073 "2023-03-18T10:52:32Z")

</div>

There seem to be several performance-related issues with closures (boxed variables) that can be tricky to understand when they will arise. Should closures be avoided as a rule? What is an alternative design pattern? They…

---

## [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…

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