# \#macros

**URL:** https://discourse.julialang.org/tag/macros/67.md?match_all_tags=true&page=2&tags%5B%5D=macro

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

**Page:** 3

---

## [Macros and piping](https://discourse.julialang.org/t/macros-and-piping/116130)

<div class="topic-metadata">

**Author:** [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Replies:** 14\
**Last updated:** [June 25, 2024, 11:58am UTC](https://discourse.julialang.org/t/macros-and-piping/116130 "2024-06-25T11:58:57Z")

</div>

How come these two examples don’t work. using Pipe, Chain macro test(x) x end @pipe 1 |\> @test(\_) #works @pipe 1 |\> @test \_ #works @pipe 1 |\> @test #doesn't work @chain 1 begin @test(\_) …

---

## [I'm on step zero on macros](https://discourse.julialang.org/t/im-on-step-zero-on-macros/115955)

<div class="topic-metadata">

**Author:** [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Replies:** 2\
**Last updated:** [June 21, 2024, 2:43am UTC](https://discourse.julialang.org/t/im-on-step-zero-on-macros/115955 "2024-06-21T02:43:16Z")

</div>

Hi, I have read most of the documentation, and trying to get my head around macros and how to write my own, but I read a thing like this macro css\_str(x::String) kvalues::Vector{SubString} = split(x, ";") Dict…

---

## [Query regarding namespaces when adding a method using a macro](https://discourse.julialang.org/t/query-regarding-namespaces-when-adding-a-method-using-a-macro/115392)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 4\
**Last updated:** [June 10, 2024, 2:07pm UTC](https://discourse.julialang.org/t/query-regarding-namespaces-when-adding-a-method-using-a-macro/115392 "2024-06-10T14:07:24Z")

</div>

I am confused about how namespaces are being applied in the following code, and would appreciate some help. module A using LinearAlgebra abstract type AbsType end struct SubType \<: AbsType end macro newmethod() quo…

---

## [@kwdef constructor not available outside of module](https://discourse.julialang.org/t/kwdef-constructor-not-available-outside-of-module/114675)

<div class="topic-metadata">

**Author:** [@veddox](https://discourse.julialang.org/u/veddox)\
**Replies:** 4\
**Last updated:** [May 31, 2024, 8:59am UTC](https://discourse.julialang.org/t/kwdef-constructor-not-available-outside-of-module/114675 "2024-05-31T08:59:00Z")

</div>

Hi everyone, working on an ecological model, I have a macro @species that creates a new struct using @kwdef. In most use cases, this works just fine. However, if I use this macro outside of the module in which it was de…

---

## [CodeSculptor.jl: take AST's apart, play with them, and put them back together. A significant upgrade to MacroTools.splitdef() and splitarg()](https://discourse.julialang.org/t/codesculptor-jl-take-asts-apart-play-with-them-and-put-them-back-together-a-significant-upgrade-to-macrotools-splitdef-and-splitarg/113476)

<div class="topic-metadata">

**Author:** [@heyx3](https://discourse.julialang.org/u/heyx3)\
**Replies:** 0\
**Last updated:** [April 24, 2024, 9:13pm UTC](https://discourse.julialang.org/t/codesculptor-jl-take-asts-apart-play-with-them-and-put-them-back-together-a-significant-upgrade-to-macrotools-splitdef-and-splitarg/113476 "2024-04-24T21:13:34Z")

</div>

---

## [Guidelines on syntax checking for macros](https://discourse.julialang.org/t/guidelines-on-syntax-checking-for-macros/113258)

<div class="topic-metadata">

**Author:** [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Replies:** 1\
**Last updated:** [April 20, 2024, 4:51am UTC](https://discourse.julialang.org/t/guidelines-on-syntax-checking-for-macros/113258 "2024-04-20T04:51:25Z")

</div>

Currently I’m developing a few macros that behave a bit like language keywords (there is no public non-macro interface). I’ve been leaning towards aggressively checking the syntax/expressions that the user provides and t…

---

## [Macros: Mutate input expressions or not?](https://discourse.julialang.org/t/macros-mutate-input-expressions-or-not/112495)

<div class="topic-metadata">

**Author:** [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Replies:** 0\
**Last updated:** [April 3, 2024, 11:45pm UTC](https://discourse.julialang.org/t/macros-mutate-input-expressions-or-not/112495 "2024-04-03T23:45:43Z")

</div>

Is there any advice on whether it’s ok to mutate the input expressions to a macro? Is it better practice to create new Expr objects? What are the pros and cons of mutating or not mutating? Below is a simple example with…

---

## [How to pass an arbitrary number of macro arguments from one macro to another? (a bit like splatting)](https://discourse.julialang.org/t/how-to-pass-an-arbitrary-number-of-macro-arguments-from-one-macro-to-another-a-bit-like-splatting/112288)

<div class="topic-metadata">

**Author:** [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Replies:** 7\
**Last updated:** [March 29, 2024, 11:27pm UTC](https://discourse.julialang.org/t/how-to-pass-an-arbitrary-number-of-macro-arguments-from-one-macro-to-another-a-bit-like-splatting/112288 "2024-03-29T23:27:05Z")

</div>

I have a macro which does stuff depending on how many inputs there are macro my\_macro(arg1) println("We got one argument.") sum = 1 if arg1 isa Int64 sum = sum + arg1 end sum end macro my\_mac…

---

## [Macros not evaluating as per expectation](https://discourse.julialang.org/t/macros-not-evaluating-as-per-expectation/112252)

<div class="topic-metadata">

**Author:** [@kishaloy](https://discourse.julialang.org/u/kishaloy)\
**Replies:** 12\
**Last updated:** [March 29, 2024, 11:21am UTC](https://discourse.julialang.org/t/macros-not-evaluating-as-per-expectation/112252 "2024-03-29T11:21:01Z")

</div>

I am facing a problem in understanding why my macro works the way it does. I have the following macro macro create\_m(m) y = :(a + b \* c + 1) y.args\[3\].args\[2\] = :(2 \* $m) dxr = m global dxy = 2 \* m …

---

## [Testing a string macro](https://discourse.julialang.org/t/testing-a-string-macro/112244)

<div class="topic-metadata">

**Author:** [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Replies:** 10\
**Last updated:** [March 29, 2024, 9:12am UTC](https://discourse.julialang.org/t/testing-a-string-macro/112244 "2024-03-29T09:12:30Z")

</div>

The code using Test xx = Dict{String,String}() macro x\_str(str,suf) return str \* xx\[suf\] end @testset "a" begin xx\["a"\] = "apple" @test x"tasty "a == "tasty apple" end Throws an error: ERROR: LoadError:…

---

## [Nice macro errors with JuliaSyntax](https://discourse.julialang.org/t/nice-macro-errors-with-juliasyntax/112017)

<div class="topic-metadata">

**Author:** [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)\
**Replies:** 2\
**Last updated:** [March 23, 2024, 9:32pm UTC](https://discourse.julialang.org/t/nice-macro-errors-with-juliasyntax/112017 "2024-03-23T21:32:08Z")

</div>

Now with the introduce of JuliaSyntax in newer versions of julia, we get much more friendly syntax error messages. For example: julia\> foo () ERROR: ParseError: # Error @ REPL\[28\]:1:4 foo () # ╙ ── whitespace is not al…

---

## [How to efficiently skip calculating some vector elements?](https://discourse.julialang.org/t/how-to-efficiently-skip-calculating-some-vector-elements/110915)

<div class="topic-metadata">

**Author:** [@g2g](https://discourse.julialang.org/u/g2g)\
**Replies:** 4\
**Last updated:** [February 28, 2024, 8:49pm UTC](https://discourse.julialang.org/t/how-to-efficiently-skip-calculating-some-vector-elements/110915 "2024-02-28T20:49:39Z")

</div>

There are significant efficiency gains by implementing Projective Geometric Algebra (PGA) in OpenGL shader hardware as described in Steven De Keninck’s recent paper, “Look, Ma, No Matrices!” Less significant (because it…

---

## [Advanced Metaprogramming Uses of QuoteNode](https://discourse.julialang.org/t/advanced-metaprogramming-uses-of-quotenode/110708)

<div class="topic-metadata">

**Author:** [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Replies:** 1\
**Last updated:** [February 24, 2024, 10:28pm UTC](https://discourse.julialang.org/t/advanced-metaprogramming-uses-of-quotenode/110708 "2024-02-24T22:28:37Z")

</div>

Continuing the discussion from Is there a better way to write this macro?: Quoth The Manual: The parser yields QuoteNodes for simple quoted items like symbols. QuoteNode can also be used for certain advanced metapro…

---

## [Is there a better way to write this macro?](https://discourse.julialang.org/t/is-there-a-better-way-to-write-this-macro/110610)

<div class="topic-metadata">

**Author:** [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Replies:** 35\
**Last updated:** [February 24, 2024, 7:09pm UTC](https://discourse.julialang.org/t/is-there-a-better-way-to-write-this-macro/110610 "2024-02-24T19:09:51Z")

</div>

I’m working on a VM, of the classic dispatch-on-value sort, and the last step (which I’m bearing down on) will be to make it type stable. The current implementation uses a multimethod for dispatch, but the Vector holding…

---

## [Nice printing of some values of an array](https://discourse.julialang.org/t/nice-printing-of-some-values-of-an-array/109512)

<div class="topic-metadata">

**Author:** [@Gravlax](https://discourse.julialang.org/u/Gravlax)\
**Replies:** 5\
**Last updated:** [January 31, 2024, 5:38pm UTC](https://discourse.julialang.org/t/nice-printing-of-some-values-of-an-array/109512 "2024-01-31T17:38:25Z")

</div>

During a simulation, it can be useful to monitor some representative values of an array (mean, max, min, median). With a colleague, we’ve tried to write a function or macro that prints the minimum and maximum value of a…

---

## [Macro CheatSheet for Julia 2024.01](https://discourse.julialang.org/t/macro-cheatsheet-for-julia-2024-01/109291)

<div class="topic-metadata">

**Author:** [@Marcell\_Havlik](https://discourse.julialang.org/u/Marcell_Havlik)\
**Replies:** 2\
**Last updated:** [January 27, 2024, 8:51pm UTC](https://discourse.julialang.org/t/macro-cheatsheet-for-julia-2024-01/109291 "2024-01-27T20:51:56Z")

</div>

Hey Julianners, I created a not so hard to read and hopefully clean Julia Cheat Sheet for macros. This is I think a very good way to fill the gaps on the harder parts. JuliaMacroCheatSheet It is worth to check it her…

---

## [Generating types using a macro from XML schema](https://discourse.julialang.org/t/generating-types-using-a-macro-from-xml-schema/107161)

<div class="topic-metadata">

**Author:** [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Replies:** 0\
**Last updated:** [December 5, 2023, 12:04pm UTC](https://discourse.julialang.org/t/generating-types-using-a-macro-from-xml-schema/107161 "2023-12-05T12:04:09Z")

</div>

Hi all, I’ve been scratching my head on this all morning, and I can’t figure what’s missing… I’m trying to define types based on the contents of an xml file and its schema. This is not unlike Trouble understanding macros…

---

## [Dynamically defining a struct with default values](https://discourse.julialang.org/t/dynamically-defining-a-struct-with-default-values/106855)

<div class="topic-metadata">

**Author:** [@Titas22](https://discourse.julialang.org/u/Titas22)\
**Replies:** 3\
**Last updated:** [November 28, 2023, 8:09pm UTC](https://discourse.julialang.org/t/dynamically-defining-a-struct-with-default-values/106855 "2023-11-28T20:09:07Z")

</div>

Hi, I would like to be able to define a struct with some default values based on an input Dict. The input is something like this: d = Dict("A"=\>1, "B"=\>2, "C"=\>"x", "D"=\>\[1,2\]) And I would like to define a struct lik…

---

## [How to generate a customized \`for\` loop code?](https://discourse.julialang.org/t/how-to-generate-a-customized-for-loop-code/106552)

<div class="topic-metadata">

**Author:** [@frankwswang](https://discourse.julialang.org/u/frankwswang)\
**Replies:** 6\
**Last updated:** [November 23, 2023, 8:01am UTC](https://discourse.julialang.org/t/how-to-generate-a-customized-for-loop-code/106552 "2023-11-23T08:01:17Z")

</div>

I’m using the macro @hyperopt from package hyperopt.jl to optimize some hyperparameters for a machine learning model. However, I’m pretty constrained by the syntax of @hyperopt, which is designed to be used as follows: h…

---

## [Passing variables to @macros](https://discourse.julialang.org/t/passing-variables-to-macros/106245)

<div class="topic-metadata">

**Author:** [@preston-johnson](https://discourse.julialang.org/u/preston-johnson)\
**Replies:** 2\
**Last updated:** [November 15, 2023, 3:11pm UTC](https://discourse.julialang.org/t/passing-variables-to-macros/106245 "2023-11-15T15:11:37Z")

</div>

Greetings, friends: (Preface) Excel is outdated. I thought I loved LET() and LAMBDA(), but I can no longer spend my waking life with VBA and reflectionless formulas. Just in the same way that Julia was created to satis…

---

## [Macro that takes a function and calls it repeatedly with different data](https://discourse.julialang.org/t/macro-that-takes-a-function-and-calls-it-repeatedly-with-different-data/106237)

<div class="topic-metadata">

**Author:** [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Replies:** 8\
**Last updated:** [November 15, 2023, 3:09am UTC](https://discourse.julialang.org/t/macro-that-takes-a-function-and-calls-it-repeatedly-with-different-data/106237 "2023-11-15T03:09:57Z")

</div>

I’m trying to write a macro that provides a convenient way for a user to compute certain measures/statistics across different levels of aggregation. For example, there are roughly 3,200 counties in the USA and we have di…

---

## [How to properly import a macro inside a begin-end block](https://discourse.julialang.org/t/how-to-properly-import-a-macro-inside-a-begin-end-block/106037)

<div class="topic-metadata">

**Author:** [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Replies:** 6\
**Last updated:** [November 12, 2023, 7:09pm UTC](https://discourse.julialang.org/t/how-to-properly-import-a-macro-inside-a-begin-end-block/106037 "2023-11-12T19:09:42Z")

</div>

Hi there, I am very surprised that the following fails in Julia 1.9 julia\> begin import Crayons: Crayon, @crayon\_str f() = crayon"dark\_gray" end ERROR: UndefVarError: \`@crayon\_str\` not defin…

---

## [Modifying Julia code using macros to substitute variables](https://discourse.julialang.org/t/modifying-julia-code-using-macros-to-substitute-variables/105921)

<div class="topic-metadata">

**Author:** [@Akhil\_Akkapelli](https://discourse.julialang.org/u/Akhil_Akkapelli)\
**Replies:** 5\
**Last updated:** [November 8, 2023, 4:27am UTC](https://discourse.julialang.org/t/modifying-julia-code-using-macros-to-substitute-variables/105921 "2023-11-08T04:27:42Z")

</div>

I’m trying to create a Julia macro to substitute specific variables in a Julia program with new ones. I have a working example, but I’m facing a challenge when I need to reassign variables within nested functions. Here’…

---

## [Providing @testset option with macro depending on Julia version](https://discourse.julialang.org/t/providing-testset-option-with-macro-depending-on-julia-version/105689)

<div class="topic-metadata">

**Author:** [@AnHeuermann](https://discourse.julialang.org/u/AnHeuermann)\
**Replies:** 1\
**Last updated:** [November 4, 2023, 6:57am UTC](https://discourse.julialang.org/t/providing-testset-option-with-macro-depending-on-julia-version/105689 "2023-11-04T06:57:08Z")

</div>

I want to use Test.@testset with option failfast=true in my unit testing. But this feature requires at least Julia 1.9 and I’m testing on both newer and older versions of Julia. I would like to define a macro or functio…

---

## [Define macros depending on optimization level](https://discourse.julialang.org/t/define-macros-depending-on-optimization-level/105714)

<div class="topic-metadata">

**Author:** [@mancolric](https://discourse.julialang.org/u/mancolric)\
**Replies:** 3\
**Last updated:** [November 2, 2023, 7:45pm UTC](https://discourse.julialang.org/t/define-macros-depending-on-optimization-level/105714 "2023-11-02T19:45:45Z")

</div>

I would like to define the macro const var"@lvec" = const var"@turbo @." if Julia is started with optimization levels 2 or 3, and const var"@lvec" = const var"@." otherwise. My main motivation is that I use @tturbo @. v…

---

## [Unpacking of variables that do not exist](https://discourse.julialang.org/t/unpacking-of-variables-that-do-not-exist/105575)

<div class="topic-metadata">

**Author:** [@johannesnauta](https://discourse.julialang.org/u/johannesnauta)\
**Replies:** 11\
**Last updated:** [October 31, 2023, 9:08am UTC](https://discourse.julialang.org/t/unpacking-of-variables-that-do-not-exist/105575 "2023-10-31T09:08:46Z")

</div>

I am trying to optimise Catalyst code by omitting many reactions as their rates will, eventually, be put to zero. Briefly, I am considering systems of which many possible reactions will have a rate of 0. To avoid creatin…

---

## [Replacing Variable Name with a String in a Julia Macro](https://discourse.julialang.org/t/replacing-variable-name-with-a-string-in-a-julia-macro/105579)

<div class="topic-metadata">

**Author:** [@Akhil\_Akkapelli](https://discourse.julialang.org/u/Akhil_Akkapelli)\
**Replies:** 1\
**Last updated:** [October 30, 2023, 5:07pm UTC](https://discourse.julialang.org/t/replacing-variable-name-with-a-string-in-a-julia-macro/105579 "2023-10-30T17:07:19Z")

</div>

I’m working on a project where I need to create a macro in Julia that takes a Julia code expression, a variable name, and a string as input and returns the same expression with the specified variable name replaced by the…

---

## [Generating a macro call from a macro with the macro name as a symbol](https://discourse.julialang.org/t/generating-a-macro-call-from-a-macro-with-the-macro-name-as-a-symbol/105335)

<div class="topic-metadata">

**Author:** [@awiersdorf](https://discourse.julialang.org/u/awiersdorf)\
**Replies:** 2\
**Last updated:** [October 24, 2023, 5:49pm UTC](https://discourse.julialang.org/t/generating-a-macro-call-from-a-macro-with-the-macro-name-as-a-symbol/105335 "2023-10-24T17:49:25Z")

</div>

I am trying to write a macro that expands into a call to another macro. Moreover, that other macro isn’t known statically—it’s determined at macro expansion time. I’m having difficulty constructing an AST with the macro …

---

## [How to discourage macros: disable automerge of General registry PRs?](https://discourse.julialang.org/t/how-to-discourage-macros-disable-automerge-of-general-registry-prs/104529)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 41\
**Last updated:** [October 19, 2023, 9:49pm UTC](https://discourse.julialang.org/t/how-to-discourage-macros-disable-automerge-of-general-registry-prs/104529 "2023-10-19T21:49:32Z")

</div>

It seems to me that macros are defined much too often in the Julia ecosystem and the (error-prone) definitions of such macros are often very complex. FWIW, the Julia Manual page on metaprogramming already heavily discou…

---

## [Macro to Extend Function with Additional Arguements (MacroTools.jl)?](https://discourse.julialang.org/t/macro-to-extend-function-with-additional-arguements-macrotools-jl/104916)

<div class="topic-metadata">

**Author:** [@matthew3245](https://discourse.julialang.org/u/matthew3245)\
**Replies:** 3\
**Last updated:** [October 13, 2023, 7:19pm UTC](https://discourse.julialang.org/t/macro-to-extend-function-with-additional-arguements-macrotools-jl/104916 "2023-10-13T19:19:28Z")

</div>

My goal is to create a macro that takes an arbitrary function and adds additional arguments and changes the docstring. For example, say I define the following function: "This is my function" function f(a, b; x=1.0, y=1…

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

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