# \#metaprogramming

**URL:** https://discourse.julialang.org/tag/metaprogramming/68.md

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

---

## [Creating a macro to call @tullio with arbitrary dimensions](https://discourse.julialang.org/t/creating-a-macro-to-call-tullio-with-arbitrary-dimensions/135848)

<div class="topic-metadata">

**Author:** [@nicrummel](https://discourse.julialang.org/u/nicrummel)\
**Replies:** 2\
**Last updated:** [April 20, 2026, 11:21am UTC](https://discourse.julialang.org/t/creating-a-macro-to-call-tullio-with-arbitrary-dimensions/135848 "2026-04-20T11:21:27Z")

</div>

Hello everyone, Custom Macro I am hoping to be able to write a macro that can call @tullio to specify an arbitrary sized linear form. I think this should follow from some easy-ish fix similar to an old thread… but I am …

---

## [Testing for warnings emitted at macro expansion time](https://discourse.julialang.org/t/testing-for-warnings-emitted-at-macro-expansion-time/136091)

<div class="topic-metadata">

**Author:** [@impact-basin](https://discourse.julialang.org/u/impact-basin)\
**Replies:** 2\
**Last updated:** [March 8, 2026, 9:07pm UTC](https://discourse.julialang.org/t/testing-for-warnings-emitted-at-macro-expansion-time/136091 "2026-03-08T21:07:56Z")

</div>

Hi all, I’m working on some macros for PikaParser.jl grammars (very WIP!), and I had a question regarding their testing. One thing I’m trying to do is build the grammar at macro-time; this allows us to return a lambda …

---

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

---

## [Value Dispatch Macro](https://discourse.julialang.org/t/value-dispatch-macro/133435)

<div class="topic-metadata">

**Author:** [@csvance](https://discourse.julialang.org/u/csvance)\
**Replies:** 4\
**Last updated:** [October 26, 2025, 8:57pm UTC](https://discourse.julialang.org/t/value-dispatch-macro/133435 "2025-10-26T20:57:07Z")

</div>

I’ve been messing around with the idea of streamlining dynamic dispatch on values for non hot path / glue code. The pattern is for multiple dispatch to dispatch to a function I define with a macro based on the known type…

---

## [My macro generates no code (?)](https://discourse.julialang.org/t/my-macro-generates-no-code/133416)

<div class="topic-metadata">

**Author:** [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Replies:** 2\
**Last updated:** [October 24, 2025, 12:25pm UTC](https://discourse.julialang.org/t/my-macro-generates-no-code/133416 "2025-10-24T12:25:18Z")

</div>

I am quite stumped! I have a macro, complicated yes, but I believe you can ignore most of its code, and still help me: If at the end of the macro definition, I @show the code it is to generate, it looks perfect. But i…

---

## [What do generated functions actually disallow?](https://discourse.julialang.org/t/what-do-generated-functions-actually-disallow/133294)

<div class="topic-metadata">

**Author:** [@Benny](https://discourse.julialang.org/u/Benny)\
**Replies:** 5\
**Last updated:** [October 21, 2025, 6:46am UTC](https://discourse.julialang.org/t/what-do-generated-functions-actually-disallow/133294 "2025-10-21T06:46:12Z")

</div>

The Manual states the following restrictions: Generated functions are only permitted to call functions that were defined before the definition of the generated function. (Failure to follow this may result in getting M…

---

## [Using macros to generate NamedTuples](https://discourse.julialang.org/t/using-macros-to-generate-namedtuples/30947)

<div class="topic-metadata">

**Author:** [@vilim](https://discourse.julialang.org/u/vilim)\
**Replies:** 17\
**Last updated:** [September 29, 2025, 5:28pm UTC](https://discourse.julialang.org/t/using-macros-to-generate-namedtuples/30947 "2025-09-29T17:28:17Z")

</div>

I am a bit confused by using macros to generate expressions. I would like to have a macro to make NamedTuples as follows: a = 1 b = 2 @make\_nt a b which should return an expression that evaluates to NamedTuple{(:a, :b)…

---

## [Is the \`value\` field of \`QuoteNode\` public?](https://discourse.julialang.org/t/is-the-value-field-of-quotenode-public/130495)

<div class="topic-metadata">

**Author:** [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Replies:** 1\
**Last updated:** [July 8, 2025, 9:12am UTC](https://discourse.julialang.org/t/is-the-value-field-of-quotenode-public/130495 "2025-07-08T09:12:20Z")

</div>

Is the value field of a QuoteNode public? The docstring does not mention the value field. I just wrote the following function, but I’m not sure if using the value field means that I’m accessing internals. function is\_sy…

---

## [Returning module from a macro: why do we need :toplevel?](https://discourse.julialang.org/t/returning-module-from-a-macro-why-do-we-need-toplevel/130346)

<div class="topic-metadata">

**Author:** [@moble](https://discourse.julialang.org/u/moble)\
**Replies:** 0\
**Last updated:** [June 30, 2025, 7:45pm UTC](https://discourse.julialang.org/t/returning-module-from-a-macro-why-do-we-need-toplevel/130346 "2025-06-30T19:45:18Z")

</div>

This may have worked in the early days, and as claimed here worked in Julia 1.4: macro mymodule(name) modname = esc(name) return :(module $modname end) end @mymodule MyMod But then it didn’t work in 1.5, and do…

---

## [Finding \`Expr\`s with docstrings](https://discourse.julialang.org/t/finding-expr-s-with-docstrings/130214)

<div class="topic-metadata">

**Author:** [@moble](https://discourse.julialang.org/u/moble)\
**Replies:** 3\
**Last updated:** [June 25, 2025, 9:30pm UTC](https://discourse.julialang.org/t/finding-expr-s-with-docstrings/130214 "2025-06-25T21:30:49Z")

</div>

How do I detect when an Expr has a docstring? For example, if expr = quote """Here's a docstring!""" const a=1 end I can look at the result to see that it is translated into a macrocall of @doc (after skipping…

---

## [Optional argument in macro](https://discourse.julialang.org/t/optional-argument-in-macro/130057)

<div class="topic-metadata">

**Author:** [@alex180500](https://discourse.julialang.org/u/alex180500)\
**Replies:** 8\
**Last updated:** [June 24, 2025, 12:46pm UTC](https://discourse.julialang.org/t/optional-argument-in-macro/130057 "2025-06-24T12:46:48Z")

</div>

I’m creating my custom progress bar for my ML related loop. It’s based on ProgressMeter.jl. The macro takes an argument as input that is a dictionary associating names to vectors of data that I’m saving during the comput…

---

## [Metaprogramming with list comprehensions](https://discourse.julialang.org/t/metaprogramming-with-list-comprehensions/129943)

<div class="topic-metadata">

**Author:** [@rokke](https://discourse.julialang.org/u/rokke)\
**Replies:** 11\
**Last updated:** [June 17, 2025, 11:29am UTC](https://discourse.julialang.org/t/metaprogramming-with-list-comprehensions/129943 "2025-06-17T11:29:47Z")

</div>

I feel like I’m always writing \[foo(a,b,c) for a, b, c in 1:n\] when I mean \[foo(a,b,c) for a in 1:n, b in 1:n, c in 1:n\], so I’d like to add something to my startup.jl to make it so. I feel like I’ve seen conflicting in…

---

## [Function compiles twice but is called just once](https://discourse.julialang.org/t/function-compiles-twice-but-is-called-just-once/128536)

<div class="topic-metadata">

**Author:** [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Replies:** 4\
**Last updated:** [April 29, 2025, 10:09pm UTC](https://discourse.julialang.org/t/function-compiles-twice-but-is-called-just-once/128536 "2025-04-29T22:09:48Z")

</div>

Consider the following code: julia\> @generated function f(x) @info "Compiling f" return :(x + 1) end f (generic function with 1 method) julia\> function type\_unstable(y::Vector{Number}) …

---

## [How do I concatenate symbols containing curly brackets into a single symbol?](https://discourse.julialang.org/t/how-do-i-concatenate-symbols-containing-curly-brackets-into-a-single-symbol/127759)

<div class="topic-metadata">

**Author:** [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Replies:** 4\
**Last updated:** [April 6, 2025, 9:18pm UTC](https://discourse.julialang.org/t/how-do-i-concatenate-symbols-containing-curly-brackets-into-a-single-symbol/127759 "2025-04-06T21:18:28Z")

</div>

I’d like to obtain the code struct S{𝒯\<:Real} end programmatically. Method 1 below works, but I’d like to be able to feed in the variable name (here S) automatically (e.g. by passing a variable x containing :S) and he…

---

## [@defined "not working" inside macro](https://discourse.julialang.org/t/defined-not-working-inside-macro/127621)

<div class="topic-metadata">

**Author:** [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Replies:** 8\
**Last updated:** [April 2, 2025, 12:03pm UTC](https://discourse.julialang.org/t/defined-not-working-inside-macro/127621 "2025-04-02T12:03:42Z")

</div>

Predictably, when I do metaprogramming, I end up confused. I create functions that take user-defined function as an input - very flexible. But when the user writes a script to use my function, each time they parse the …

---

## [Average multiple evaluations of the same function](https://discourse.julialang.org/t/average-multiple-evaluations-of-the-same-function/126009)

<div class="topic-metadata">

**Author:** [@ignace](https://discourse.julialang.org/u/ignace)\
**Replies:** 2\
**Last updated:** [February 17, 2025, 9:09pm UTC](https://discourse.julialang.org/t/average-multiple-evaluations-of-the-same-function/126009 "2025-02-17T21:09:41Z")

</div>

Suppose we have a function f(input) that transforms input (a long list of input arguments) into an output (a long tuple), depending on some (hidden) random values. A very minimalist example is the randn function. Is th…

---

## [Macro to substitute parameters used for JuMP model](https://discourse.julialang.org/t/macro-to-substitute-parameters-used-for-jump-model/125936)

<div class="topic-metadata">

**Author:** [@KSepetanc](https://discourse.julialang.org/u/KSepetanc)\
**Replies:** 2\
**Last updated:** [February 16, 2025, 11:27pm UTC](https://discourse.julialang.org/t/macro-to-substitute-parameters-used-for-jump-model/125936 "2025-02-16T23:27:24Z")

</div>

I read available topics on macros but still can not make working macro to substitute parameter. What I want is instead of creating new variable (for example Pmax) to use @Pmax macro that will expand @Pmax(g) to data\[:gen…

---

## [Metaprogramming to automatically test new versions of code](https://discourse.julialang.org/t/metaprogramming-to-automatically-test-new-versions-of-code/125899)

<div class="topic-metadata">

**Author:** [@JADekker](https://discourse.julialang.org/u/JADekker)\
**Replies:** 9\
**Last updated:** [February 16, 2025, 6:32am UTC](https://discourse.julialang.org/t/metaprogramming-to-automatically-test-new-versions-of-code/125899 "2025-02-16T06:32:56Z")

</div>

In writing my code, I often find that I wish to test if a certain function, say f, can be made quicker. One way of doing this is to find a place within my code where the function gets used and run f and some f\_quick base…

---

## [Local macros](https://discourse.julialang.org/t/local-macros/125539)

<div class="topic-metadata">

**Author:** [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Replies:** 4\
**Last updated:** [February 14, 2025, 6:42pm UTC](https://discourse.julialang.org/t/local-macros/125539 "2025-02-14T18:42:38Z")

</div>

In CommonLisp one can define local macros using MACROLET. Julia doesn’t have local macros, but one can imagine that a given global macro could provide local macros for use wwithin its body using a code walker and rules …

---

## [Simple question about macro arguments](https://discourse.julialang.org/t/simple-question-about-macro-arguments/125795)

<div class="topic-metadata">

**Author:** [@Ludovic\_Dumoulin](https://discourse.julialang.org/u/Ludovic_Dumoulin)\
**Replies:** 6\
**Last updated:** [February 12, 2025, 3:14pm UTC](https://discourse.julialang.org/t/simple-question-about-macro-arguments/125795 "2025-02-12T15:14:18Z")

</div>

Hello, I would like to use a macro to compute derivative of several fields. Concretly, in a simplified context, I have the following issue: for the macro macro testij(i,j) esc(:(a\[$i\]=b\[$j\] )) end for a and b define…

---

## [Macro for linking Julia fcn to @ccall of C fcn, given names and arg types](https://discourse.julialang.org/t/macro-for-linking-julia-fcn-to-ccall-of-c-fcn-given-names-and-arg-types/125326)

<div class="topic-metadata">

**Author:** [@luke-kiernan](https://discourse.julialang.org/u/luke-kiernan)\
**Replies:** 3\
**Last updated:** [January 29, 2025, 5:29pm UTC](https://discourse.julialang.org/t/macro-for-linking-julia-fcn-to-ccall-of-c-fcn-given-names-and-arg-types/125326 "2025-01-29T17:29:02Z")

</div>

I have a bunch of C functions, that I want to write Julia wrappers for. Here’s an example: const LIBSPARSE = "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/"\* "vecLib.framewor…

---

## [Invalidating the generator of a \`@generated\` function](https://discourse.julialang.org/t/invalidating-the-generator-of-a-generated-function/125164)

<div class="topic-metadata">

**Author:** [@K11BN367](https://discourse.julialang.org/u/K11BN367)\
**Replies:** 1\
**Last updated:** [January 24, 2025, 1:03pm UTC](https://discourse.julialang.org/t/invalidating-the-generator-of-a-generated-function/125164 "2025-01-24T13:03:18Z")

</div>

Hello, i started to investigate the julia internals a bit and toyed around with some code that tries to invalidate the generator of a ‘@generated’ function. I came up with the following code: import Core.Compiler.Meth…

---

## [Understanding macro lexical bindings](https://discourse.julialang.org/t/understanding-macro-lexical-bindings/125105)

<div class="topic-metadata">

**Author:** [@nietzsche](https://discourse.julialang.org/u/nietzsche)\
**Replies:** 2\
**Last updated:** [January 23, 2025, 12:59pm UTC](https://discourse.julialang.org/t/understanding-macro-lexical-bindings/125105 "2025-01-23T12:59:55Z")

</div>

Hello, the macroexpand in the code below lead to an Error. UndefVarError: i not defined Ther error is clear, but how can I solve it? Or how can I put the “i” from the for loop in the right environment? macro dlamdba(…

---

## [Function specialization based on values, not just types](https://discourse.julialang.org/t/function-specialization-based-on-values-not-just-types/123043)

<div class="topic-metadata">

**Author:** [@benc](https://discourse.julialang.org/u/benc)\
**Replies:** 18\
**Last updated:** [November 26, 2024, 12:42pm UTC](https://discourse.julialang.org/t/function-specialization-based-on-values-not-just-types/123043 "2024-11-26T12:42:38Z")

</div>

I’m working on Markov Chains where a lot of recursive functions have a mathematical parametric equation with a base case, simplified to function f(xs, 1) @info "Base case" end function f(xs, val) @info "Gene…

---

## [Creating Parameters.jl struct samples using variable keywords](https://discourse.julialang.org/t/creating-parameters-jl-struct-samples-using-variable-keywords/121639)

<div class="topic-metadata">

**Author:** [@JPool](https://discourse.julialang.org/u/JPool)\
**Replies:** 4\
**Last updated:** [October 23, 2024, 5:06pm UTC](https://discourse.julialang.org/t/creating-parameters-jl-struct-samples-using-variable-keywords/121639 "2024-10-23T17:06:13Z")

</div>

Hi, I’m trying to make a function that takes in a default struct which holds parameters for my model. I want to be able to make a sample list where some properties are kept constant, and some are sampled using a sampling…

---

## [IDE: step-through debugging for macro-generated function?](https://discourse.julialang.org/t/ide-step-through-debugging-for-macro-generated-function/63899)

<div class="topic-metadata">

**Author:** [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Replies:** 0\
**Last updated:** [July 1, 2021, 2:59pm UTC](https://discourse.julialang.org/t/ide-step-through-debugging-for-macro-generated-function/63899 "2021-07-01T14:59:52Z")

</div>

I recently tried using VSCode debugger to “step into” a function from an external package, but VSCode pointed me to a strange code location. Eventually I figured out that the function was not written out, but was generat…

---

## [Using GLM programmatically](https://discourse.julialang.org/t/using-glm-programmatically/30828)

<div class="topic-metadata">

**Author:** [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Replies:** 8\
**Last updated:** [October 8, 2024, 8:13am UTC](https://discourse.julialang.org/t/using-glm-programmatically/30828 "2024-10-08T08:13:07Z")

</div>

I would like to perform linear regression in a loop with variables. For example, using DataFrames, GLM data = DataFrame(X=\[1,2,3\], Y=\[2,4,7\]) var1 = :Y var2 = :X ols = lm(@formula($var1 ~ $var2), data) I tried vari…

---

## [Create dynamic functions with Macro](https://discourse.julialang.org/t/create-dynamic-functions-with-macro/119824)

<div class="topic-metadata">

**Author:** [@Ronneesley](https://discourse.julialang.org/u/Ronneesley)\
**Replies:** 15\
**Last updated:** [September 26, 2024, 12:33am UTC](https://discourse.julialang.org/t/create-dynamic-functions-with-macro/119824 "2024-09-26T00:33:22Z")

</div>

Hi, I’m trying create “dynamic” functions with macro, but it isn’t working. Let-me explain: I’ve this function: function f(::Type{T}, message) where T println("Type: ", T) println("Message: ", message) end Wh…

---

## [Verifiable computing in julia](https://discourse.julialang.org/t/verifiable-computing-in-julia/119067)

<div class="topic-metadata">

**Author:** [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Replies:** 5\
**Last updated:** [September 5, 2024, 2:26pm UTC](https://discourse.julialang.org/t/verifiable-computing-in-julia/119067 "2024-09-05T14:26:07Z")

</div>

An upcoming field is verifiable computing. The typical topic is that you provide a proof that you have run a program with an input which resulted in a particular output. Even without revealing the input. E.g. f(x::BigIn…

---

## [Retrieve unevaluated symbol inside \`@eval\`](https://discourse.julialang.org/t/retrieve-unevaluated-symbol-inside-eval/118978)

<div class="topic-metadata">

**Author:** [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Replies:** 1\
**Last updated:** [September 3, 2024, 9:23am UTC](https://discourse.julialang.org/t/retrieve-unevaluated-symbol-inside-eval/118978 "2024-09-03T09:23:56Z")

</div>

In one of my packages I use @eval to define a list of functions in a loop based on symbols. I would also like to use the symbol without evaluating it, but I can’t figure out how. Maybe it’s an XY problem? julia\> for s i…

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