# \#macros

**URL:** https://discourse.julialang.org/tag/macros/67.md

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

---

## [How to return a macro from a macro?](https://discourse.julialang.org/t/how-to-return-a-macro-from-a-macro/139612)

<div class="topic-metadata">

**Author:** [@ysaak](https://discourse.julialang.org/u/ysaak)\
**Replies:** 6\
**Last updated:** [September 23, 2026, 12:23pm UTC](https://discourse.julialang.org/t/how-to-return-a-macro-from-a-macro/139612 "2026-09-23T12:23:47Z")

</div>

I want to create a macro that receives the header foo(args) and body begin ... end and returns a macro version of it. macro macwo(header::Expr, body::Expr) #= macro header body end =# end # Impl…

---

## [New syntax for function decoration with macros](https://discourse.julialang.org/t/new-syntax-for-function-decoration-with-macros/139512)

<div class="topic-metadata">

**Author:** [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Replies:** 2\
**Last updated:** [September 17, 2026, 10:40am UTC](https://discourse.julialang.org/t/new-syntax-for-function-decoration-with-macros/139512 "2026-09-17T10:40:31Z")

</div>

I have noticed, that the Base functions now use a new syntax, where the decorator macros such as @inline or @propagate\_inbounds appear as the first line of the body of the function. What version of Julia was this synta…

---

## [Document Expr(:symbolicgoto, ...) and :symboliclabel](https://discourse.julialang.org/t/document-expr-symbolicgoto-and-symboliclabel/137267)

<div class="topic-metadata">

**Author:** [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Replies:** 10\
**Last updated:** [May 24, 2026, 1:26pm UTC](https://discourse.julialang.org/t/document-expr-symbolicgoto-and-symboliclabel/137267 "2026-05-24T13:26:13Z")

</div>

To get something equivalent to @goto foo and @label foo in a macro, one has to use Expr(:symbolicgoto, :foo) or Expr(:symbolicgoto, :foo) (see eg this and similar topics). Would it make sense to document this in the doc…

---

## [Limitations of generated functions versus macros or functions](https://discourse.julialang.org/t/limitations-of-generated-functions-versus-macros-or-functions/136910)

<div class="topic-metadata">

**Author:** [@Benny](https://discourse.julialang.org/u/Benny)\
**Replies:** 11\
**Last updated:** [April 29, 2026, 3:46pm UTC](https://discourse.julialang.org/t/limitations-of-generated-functions-versus-macros-or-functions/136910 "2026-04-29T15:46:31Z")

</div>

Tentatively writing a generated function and relearning things, and ran into a couple questions: The most warned limitation of generated functions is most side effects in the method body (throwing errors is a notable e…

---

## [Macros and Type Piracy](https://discourse.julialang.org/t/macros-and-type-piracy/136192)

<div class="topic-metadata">

**Author:** [@goerz](https://discourse.julialang.org/u/goerz)\
**Replies:** 5\
**Last updated:** [March 16, 2026, 12:49pm UTC](https://discourse.julialang.org/t/macros-and-type-piracy/136192 "2026-03-16T12:49:41Z")

</div>

Motivated by Seeking feedback on Chainables before registering it, I initially thought that that proposed new package was modifying the behavior of existing macros (like @chain from Chain.jl). I think that was a misunder…

---

## [Am I using macros properly to avoid code repetition?](https://discourse.julialang.org/t/am-i-using-macros-properly-to-avoid-code-repetition/135122)

<div class="topic-metadata">

**Author:** [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Replies:** 6\
**Last updated:** [January 19, 2026, 12:56am UTC](https://discourse.julialang.org/t/am-i-using-macros-properly-to-avoid-code-repetition/135122 "2026-01-19T00:56:58Z")

</div>

I want to define a macro inside a module MyAddConstrs, hoping that I can avoid code repetition therein. The original code is module MyAddConstrs import JuMP \_gen2(model, GD, p, t, s) = JuMP.@expression(model, sum(p…

---

## [@chaineach](https://discourse.julialang.org/t/chaineach/134423)

<div class="topic-metadata">

**Author:** [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Replies:** 12\
**Last updated:** [December 9, 2025, 12:10am UTC](https://discourse.julialang.org/t/chaineach/134423 "2025-12-09T00:10:13Z")

</div>

I’d like a macro @chaineach such that: @chaineach x begin #commands end is equivalent to map( y -\> @chain y begin #commands end, collect(x) ) Example usage is a @chain block processing a DataFrame and creati…

---

## [@run macro?](https://discourse.julialang.org/t/run-macro/133861)

<div class="topic-metadata">

**Author:** [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Replies:** 9\
**Last updated:** [November 16, 2025, 5:42am UTC](https://discourse.julialang.org/t/run-macro/133861 "2025-11-16T05:42:31Z")

</div>

How could a run macro be defined that executes a script? It should just include the file. Would something like: @run myscript.jl be possible as replacement for include("myscript.jl") ? That would save 7 keystrokes …

---

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

---

## [Limit subtype to immediate parent in method signature?](https://discourse.julialang.org/t/limit-subtype-to-immediate-parent-in-method-signature/133177)

<div class="topic-metadata">

**Author:** [@bcon](https://discourse.julialang.org/u/bcon)\
**Replies:** 2\
**Last updated:** [October 16, 2025, 11:28am UTC](https://discourse.julialang.org/t/limit-subtype-to-immediate-parent-in-method-signature/133177 "2025-10-16T11:28:51Z")

</div>

In UnitTypes I need to restrict a method’s arguments to types that all subtype the same parent, though there are multiple families of parents and child types. Here’s an example: abstract type AbstractMeasure end abstr…

---

## [Macros to fill keyword arguments](https://discourse.julialang.org/t/macros-to-fill-keyword-arguments/25454)

<div class="topic-metadata">

**Author:** [@dstarerstor](https://discourse.julialang.org/u/dstarerstor)\
**Replies:** 6\
**Last updated:** [August 20, 2025, 4:40pm UTC](https://discourse.julialang.org/t/macros-to-fill-keyword-arguments/25454 "2025-08-20T16:40:10Z")

</div>

Hi everyone, I’m having a difficult time understanding how to properly use macros. I’m trying to make a macro @passkwargs that will turn foo(; @passkwargs(x,y)) into foo(; x=x, y=y) I’m just starting to work with ma…

---

## [Semantically equivalent expressions with wildly different performance](https://discourse.julialang.org/t/semantically-equivalent-expressions-with-wildly-different-performance/131123)

<div class="topic-metadata">

**Author:** [@bremez](https://discourse.julialang.org/u/bremez)\
**Replies:** 10\
**Last updated:** [July 28, 2025, 2:54pm UTC](https://discourse.julialang.org/t/semantically-equivalent-expressions-with-wildly-different-performance/131123 "2025-07-28T14:54:10Z")

</div>

I came across the following example when I needed something that beats the performance of LinearAlgebra’s build in mul!(M, x, y'). Any known reason for this huge difference in performance? Tested on 11.6. using Benchmar…

---

## [Macro utilities for working with function definitions in 2025](https://discourse.julialang.org/t/macro-utilities-for-working-with-function-definitions-in-2025/130687)

<div class="topic-metadata">

**Author:** [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Replies:** 17\
**Last updated:** [July 22, 2025, 2:20pm UTC](https://discourse.julialang.org/t/macro-utilities-for-working-with-function-definitions-in-2025/130687 "2025-07-22T14:20:53Z")

</div>

What are the recommended tools these days for working with function definition expressions in macros? I’m aware of ExprTools.jl and MacroTools.jl, but I don’t know how maintained they are and whether those packages or ot…

---

## [Macro that construct function names and call them](https://discourse.julialang.org/t/macro-that-construct-function-names-and-call-them/130532)

<div class="topic-metadata">

**Author:** [@jsjie](https://discourse.julialang.org/u/jsjie)\
**Replies:** 6\
**Last updated:** [July 8, 2025, 1:45am UTC](https://discourse.julialang.org/t/macro-that-construct-function-names-and-call-them/130532 "2025-07-08T01:45:28Z")

</div>

My actual usage is that I have a bunch of (~30) functions that performs similar but different operations depending on one argument. The most straight way to implement is: function do!(arg1, l) if l == 3 # d…

---

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

---

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

---

## [Macro-defined callable structs \`ArgumentError: invalid type for argument number 1 in method definition\`](https://discourse.julialang.org/t/macro-defined-callable-structs-argumenterror-invalid-type-for-argument-number-1-in-method-definition/129971)

<div class="topic-metadata">

**Author:** [@user664303](https://discourse.julialang.org/u/user664303)\
**Replies:** 3\
**Last updated:** [June 17, 2025, 6:07pm UTC](https://discourse.julialang.org/t/macro-defined-callable-structs-argumenterror-invalid-type-for-argument-number-1-in-method-definition/129971 "2025-06-17T18:07:39Z")

</div>

Running macro definestruct(name, ndims) return quote struct $name{T} v::Vector{T} end $name(v::Vector{T}) where T = $name{T}(v) nvars(::$name) = $ndims end end @define…

---

## [Escaping newlines in multi-line string macros?](https://discourse.julialang.org/t/escaping-newlines-in-multi-line-string-macros/129240)

<div class="topic-metadata">

**Author:** [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Replies:** 3\
**Last updated:** [May 23, 2025, 6:37am UTC](https://discourse.julialang.org/t/escaping-newlines-in-multi-line-string-macros/129240 "2025-05-23T06:37:41Z")

</div>

Would this feature, to be able to write a single string literal on multiple lines of the source code without \\n in the string, be eventually implemented for all string-like objects? I was surprised that it doesn’t wor…

---

## [Adding method to function in macro](https://discourse.julialang.org/t/adding-method-to-function-in-macro/128613)

<div class="topic-metadata">

**Author:** [@PatrickHaecker](https://discourse.julialang.org/u/PatrickHaecker)\
**Replies:** 14\
**Last updated:** [May 4, 2025, 4:49am UTC](https://discourse.julialang.org/t/adding-method-to-function-in-macro/128613 "2025-05-04T04:49:53Z")

</div>

I have a package which defines a function and some methods for this function when the package gets loaded. In addition, the package defines a macro which returns an expression which defines additional methods to this fu…

---

## [Macro to generate 2 keyword function parameters](https://discourse.julialang.org/t/macro-to-generate-2-keyword-function-parameters/128161)

<div class="topic-metadata">

**Author:** [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Replies:** 3\
**Last updated:** [April 17, 2025, 3:43pm UTC](https://discourse.julialang.org/t/macro-to-generate-2-keyword-function-parameters/128161 "2025-04-17T15:43:57Z")

</div>

Let’s say I have this function import Dates: DateTime function myfun1(; ena::Int=1, offsettime::DateTime, entrytime::DateTime, dio::Int) return nothing end with the parameters being :($(Expr(:parameters, :($(Expr(…

---

## [\[ANN\] LightSumTypes.jl v4](https://discourse.julialang.org/t/ann-lightsumtypes-jl-v4/116741)

<div class="topic-metadata">

**Author:** [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Replies:** 33\
**Last updated:** [March 26, 2025, 1:59pm UTC](https://discourse.julialang.org/t/ann-lightsumtypes-jl-v4/116741 "2025-03-26T13:59:32Z")

</div>

Hi all, I’d like to share with you that version 3 of DynamicSumTypes.jl is now out! The package allows to enclose multiple types into one with the @sumtype macro and it is almost like working with a Union of types, but …

---

## [Can anyone improve this macro?](https://discourse.julialang.org/t/can-anyone-improve-this-macro/127359)

<div class="topic-metadata">

**Author:** [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Replies:** 3\
**Last updated:** [March 26, 2025, 4:05am UTC](https://discourse.julialang.org/t/can-anyone-improve-this-macro/127359 "2025-03-26T04:05:37Z")

</div>

I have written the following macro: using Logging """ @no\_logging(code\_to\_execute) Execute \`code\_to\_execute\` sending all logging to the \[\`Logging.NullLogger\`\](@ref). """ macro no\_logging(code) return :( with\_…

---

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

---

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

---

## [Is Compat.jl worth it for the \`public\` keyword?](https://discourse.julialang.org/t/is-compat-jl-worth-it-for-the-public-keyword/119041)

<div class="topic-metadata">

**Author:** [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Replies:** 37\
**Last updated:** [February 11, 2025, 9:10pm UTC](https://discourse.julialang.org/t/is-compat-jl-worth-it-for-the-public-keyword/119041 "2025-02-11T21:10:10Z")

</div>

With the introduction of public by @Lilith, many packages will want to mark their unexported public symbols that way. Since public is only available in Julia 1.11, the current recommended way to do this in 1.10 is with t…

---

## [\_str string interpolation?](https://discourse.julialang.org/t/str-string-interpolation/125766)

<div class="topic-metadata">

**Author:** [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Replies:** 12\
**Last updated:** [February 11, 2025, 7:55pm UTC](https://discourse.julialang.org/t/str-string-interpolation/125766 "2025-02-11T19:55:43Z")

</div>

I am not sure what is the issue, but I would like to this to work: julia\> macro test\_str(s) :($(esc(s))) end @test\_str (macro with 1 method) julia\> test"a 1" # "works" "a 1" julia\> i = 1 1 julia\> t…

---

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

---

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

---

## [SQL and Macros](https://discourse.julialang.org/t/sql-and-macros/124673)

<div class="topic-metadata">

**Author:** [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Replies:** 1\
**Last updated:** [January 11, 2025, 3:59pm UTC](https://discourse.julialang.org/t/sql-and-macros/124673 "2025-01-11T15:59:21Z")

</div>

I have the following script that runs fine: using DuckDB using DBInterface using DataFrames # Global connection variable for reuse const duckdb\_connection = Ref{Union{DuckDB.DB, Nothing}}(nothing) # Function to manage…

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