# \#macros

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

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

**Page:** 4

---

## [Proper way to define macros that declare functions?](https://discourse.julialang.org/t/proper-way-to-define-macros-that-declare-functions/104058)

<div class="topic-metadata">

**Author:** [@Vincent\_Picaud](https://discourse.julialang.org/u/Vincent_Picaud)\
**Replies:** 4\
**Last updated:** [September 20, 2023, 11:18am UTC](https://discourse.julialang.org/t/proper-way-to-define-macros-that-declare-functions/104058 "2023-09-20T11:18:53Z")

</div>

I define a create\_foo macro that declares a function foo. I want to be able to declare foo functions in the module where the macro is defined. This part is OK as this minimal example shows: module Foo macro cr…

---

## [Contruct filter expression within loop](https://discourse.julialang.org/t/contruct-filter-expression-within-loop/104003)

<div class="topic-metadata">

**Author:** [@chedy](https://discourse.julialang.org/u/chedy)\
**Replies:** 2\
**Last updated:** [September 19, 2023, 7:11am UTC](https://discourse.julialang.org/t/contruct-filter-expression-within-loop/104003 "2023-09-19T07:11:48Z")

</div>

Hi all, I’m trying to simplify this code using a macro, but I’m pretty new to meta-programming and not sure what I need to do. I have an array bigrams of structs that have two fields (among others): w1, and w2 (both int…

---

## [Can expressions containing syntax errors be used in macro calls?](https://discourse.julialang.org/t/can-expressions-containing-syntax-errors-be-used-in-macro-calls/103949)

<div class="topic-metadata">

**Author:** [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Replies:** 2\
**Last updated:** [September 17, 2023, 1:57pm UTC](https://discourse.julialang.org/t/can-expressions-containing-syntax-errors-be-used-in-macro-calls/103949 "2023-09-17T13:57:59Z")

</div>

I’d like to use an expression like this one in a macro call struct A(B) x::Int end which gives a syntax error when called without the macro wrap: julia\> struct A(B) x::Int end ERROR: syntax: inva…

---

## [Argument confusion when calling macro with semicolon parameter syntax](https://discourse.julialang.org/t/argument-confusion-when-calling-macro-with-semicolon-parameter-syntax/103716)

<div class="topic-metadata">

**Author:** [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Replies:** 7\
**Last updated:** [September 13, 2023, 8:04am UTC](https://discourse.julialang.org/t/argument-confusion-when-calling-macro-with-semicolon-parameter-syntax/103716 "2023-09-13T08:04:07Z")

</div>

Applying semicolon parameter syntax generates a surprising order of expressions; parameters go in the first place, followed by the expected order of arguments julia\> macro m1(expressions...) @show expression…

---

## [Debugger does not step over quoted macro code in VS Code](https://discourse.julialang.org/t/debugger-does-not-step-over-quoted-macro-code-in-vs-code/103657)

<div class="topic-metadata">

**Author:** [@quert](https://discourse.julialang.org/u/quert)\
**Replies:** 3\
**Last updated:** [September 11, 2023, 9:02am UTC](https://discourse.julialang.org/t/debugger-does-not-step-over-quoted-macro-code-in-vs-code/103657 "2023-09-11T09:02:36Z")

</div>

I would like to know how other people use the debugger with code that uses the logging macros. For me it is almost unusable, since the debugger steps into every logging call, e.g. @debug, @info, .... Every time I have to…

---

## [MLStyle's vs MacroTools' @capture](https://discourse.julialang.org/t/mlstyles-vs-macrotools-capture/102968)

<div class="topic-metadata">

**Author:** [@deaminase](https://discourse.julialang.org/u/deaminase)\
**Replies:** 4\
**Last updated:** [September 8, 2023, 12:09am UTC](https://discourse.julialang.org/t/mlstyles-vs-macrotools-capture/102968 "2023-09-08T00:09:07Z")

</div>

Hey folks. I’ve been trying to figure out how to use MLStyle.Modules.AST.@capture for doing the same thing as MacroTools.@capture(ex, fn\_(args\_\_; kws\_\_) | fn\_(args\_\_)) However, I can’t seem to come up with the syntax th…

---

## [Macro design recommendations for \`do-end\` like semantics](https://discourse.julialang.org/t/macro-design-recommendations-for-do-end-like-semantics/103610)

<div class="topic-metadata">

**Author:** [@kdheepak](https://discourse.julialang.org/u/kdheepak)\
**Replies:** 2\
**Last updated:** [September 7, 2023, 2:17pm UTC](https://discourse.julialang.org/t/macro-design-recommendations-for-do-end-like-semantics/103610 "2023-09-07T14:17:16Z")

</div>

In Julia, there’s a function do block syntax like so: foo() do x println(x) end where foo is defined like foo(f::Function) I’m interested in writing a macro that affects a block. i.e. I want user facing code to look…

---

## [How to evaluate symbol as a "variable" inside a macro?](https://discourse.julialang.org/t/how-to-evaluate-symbol-as-a-variable-inside-a-macro/103607)

<div class="topic-metadata">

**Author:** [@Qfl3x](https://discourse.julialang.org/u/Qfl3x)\
**Replies:** 12\
**Last updated:** [September 7, 2023, 1:23pm UTC](https://discourse.julialang.org/t/how-to-evaluate-symbol-as-a-variable-inside-a-macro/103607 "2023-09-07T13:23:25Z")

</div>

Hello, I’m currently trying to implement merge on ComponentArrays. I’ve taken inspiration from NamedTuples in the stdlib here, and wrote a macro and a function: (instead of the Expr) using ComponentArrays c1 = Component…

---

## [How do I get the current variable value within a macro?](https://discourse.julialang.org/t/how-do-i-get-the-current-variable-value-within-a-macro/103487)

<div class="topic-metadata">

**Author:** [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Replies:** 1\
**Last updated:** [September 3, 2023, 4:55pm UTC](https://discourse.julialang.org/t/how-do-i-get-the-current-variable-value-within-a-macro/103487 "2023-09-03T16:55:09Z")

</div>

macro getvar(x) println(x) return nothing end x = 1 @getvar x getvar sees x as a symbol and prints out “x” as expected. Now, let’s assume I need the x value for some further logic within the macro (not within…

---

## [How is a non-standard literal different from calling its corresponding macro?](https://discourse.julialang.org/t/how-is-a-non-standard-literal-different-from-calling-its-corresponding-macro/103349)

<div class="topic-metadata">

**Author:** [@Benny](https://discourse.julialang.org/u/Benny)\
**Replies:** 8\
**Last updated:** [August 31, 2023, 11:35pm UTC](https://discourse.julialang.org/t/how-is-a-non-standard-literal-different-from-calling-its-corresponding-macro/103349 "2023-08-31T23:35:30Z")

</div>

julia\> x = "whoops" "whoops" julia\> @raw\_str("$x") # huh... ERROR: UndefVarError: x not defined Stacktrace: \[1\] top-level scope @ REPL\[48\]:1 julia\> "$x", raw"$x" # expected ("whoops", "\\$x") julia\> @macroexpand @r…

---

## [Generate Input for Tullio](https://discourse.julialang.org/t/generate-input-for-tullio/102859)

<div class="topic-metadata">

**Author:** [@AlexanderNenninger](https://discourse.julialang.org/u/AlexanderNenninger)\
**Replies:** 1\
**Last updated:** [August 16, 2023, 1:27pm UTC](https://discourse.julialang.org/t/generate-input-for-tullio/102859 "2023-08-16T13:27:48Z")

</div>

I’d like to benchmark my own code for working with N-dimensional arrays. As baseline, Tullio seems to be the fastest, significantly faster than sum. Given the setup using BenchmarkTools using Tullio const M = 3 const …

---

## [Undefined variable Error in a quote block](https://discourse.julialang.org/t/undefined-variable-error-in-a-quote-block/102404)

<div class="topic-metadata">

**Author:** [@shawn\_T](https://discourse.julialang.org/u/shawn_T)\
**Replies:** 1\
**Last updated:** [August 2, 2023, 8:09pm UTC](https://discourse.julialang.org/t/undefined-variable-error-in-a-quote-block/102404 "2023-08-02T20:09:33Z")

</div>

Hi all, I was designing a macro and the code below is the return line of the macro, but it reports “i not defined”, I think it happens at $(result\[i\]) because once I replace it with other expr it is fine, so is there a …

---

## [@testset expands all macros \*before\* executing the generated code](https://discourse.julialang.org/t/testset-expands-all-macros-before-executing-the-generated-code/102197)

<div class="topic-metadata">

**Author:** [@iago-lito](https://discourse.julialang.org/u/iago-lito)\
**Replies:** 3\
**Last updated:** [July 31, 2023, 8:52am UTC](https://discourse.julialang.org/t/testset-expands-all-macros-before-executing-the-generated-code/102197 "2023-07-31T08:52:15Z")

</div>

Hello community. I’ve just spent hours confusingly revolving around the following quirk. Given this code in my package: macro a() println("expand a") :(println("exec a")) end macro b() println("expand b") …

---

## [Creating a variable inside a macro with specified name](https://discourse.julialang.org/t/creating-a-variable-inside-a-macro-with-specified-name/102187)

<div class="topic-metadata">

**Author:** [@shawn\_T](https://discourse.julialang.org/u/shawn_T)\
**Replies:** 7\
**Last updated:** [July 28, 2023, 5:44pm UTC](https://discourse.julialang.org/t/creating-a-variable-inside-a-macro-with-specified-name/102187 "2023-07-28T17:44:50Z")

</div>

Hi all, I am trying to create a variable inside a Julia macro, then return the variable to the REPL, the variable should also have the name I specified from the macro input argument. I have two tests, the first test fun…

---

## [Programatic function (and variable) names](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861)

<div class="topic-metadata">

**Author:** [@Anton\_Hinneck](https://discourse.julialang.org/u/Anton_Hinneck)\
**Replies:** 16\
**Last updated:** [July 22, 2023, 12:07am UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861 "2023-07-22T00:07:06Z")

</div>

I have a use case, where code needs to generate multiple sets of functions. So functions do not get overwritten, I want to add a unique pre-/postfix equal for each function in a set. Specifically, functions init() ru…

---

## [\[ANN\] Announcing ForwardMethods.jl](https://discourse.julialang.org/t/ann-announcing-forwardmethods-jl/100902)

<div class="topic-metadata">

**Author:** [@curtd](https://discourse.julialang.org/u/curtd)\
**Replies:** 1\
**Last updated:** [July 4, 2023, 4:19pm UTC](https://discourse.julialang.org/t/ann-announcing-forwardmethods-jl/100902 "2023-07-04T16:19:05Z")

</div>

I’m happy to announce ForwardMethods.jl v1.1, a package made to help remove some boilerplate when defining forwarded/delegated methods. Method Definition Forwarding Concretely, suppose you have a composite type struct …

---

## [How to pass name of macro argument into result struct parameter](https://discourse.julialang.org/t/how-to-pass-name-of-macro-argument-into-result-struct-parameter/101049)

<div class="topic-metadata">

**Author:** [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Replies:** 1\
**Last updated:** [July 1, 2023, 10:36am UTC](https://discourse.julialang.org/t/how-to-pass-name-of-macro-argument-into-result-struct-parameter/101049 "2023-07-01T10:36:47Z")

</div>

Hello, please help to pass name of macro argument into result struct parameter struct Foo{x} end macro foo(x) return esc(:($x = Foo{QuoteNode($x)})) # ??? end @macroexpand @foo x # need - \> # x = Foo{:x} x = …

---

## [Macro to create a generated function](https://discourse.julialang.org/t/macro-to-create-a-generated-function/99135)

<div class="topic-metadata">

**Author:** [@devetak-m](https://discourse.julialang.org/u/devetak-m)\
**Replies:** 3\
**Last updated:** [May 20, 2023, 7:51am UTC](https://discourse.julialang.org/t/macro-to-create-a-generated-function/99135 "2023-05-20T07:51:57Z")

</div>

Hello. I want to create a macro that reads: macro store(agent, value) f\_name = Symbol("store\_", value) f = quote @inline @generated function $f\_name(a, v) return quote a.value = v end …

---

## [Runtime initializaion of parametric type in package from input](https://discourse.julialang.org/t/runtime-initializaion-of-parametric-type-in-package-from-input/99080)

<div class="topic-metadata">

**Author:** [@FireCrumb](https://discourse.julialang.org/u/FireCrumb)\
**Replies:** 0\
**Last updated:** [May 18, 2023, 6:33pm UTC](https://discourse.julialang.org/t/runtime-initializaion-of-parametric-type-in-package-from-input/99080 "2023-05-18T18:33:37Z")

</div>

Hi, I have a package, where I want to define MyStaticVectorType type, as an alias to SVector{MEM\_SIZE,Int} (from StaticArrays.jl). Where MEM\_SIZE is loaded from environment variable when the program runs, so it’s avail…

---

## [Is there a shorthand to make all fields of a struct the same type?](https://discourse.julialang.org/t/is-there-a-shorthand-to-make-all-fields-of-a-struct-the-same-type/98961)

<div class="topic-metadata">

**Author:** [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Replies:** 1\
**Last updated:** [May 16, 2023, 11:20pm UTC](https://discourse.julialang.org/t/is-there-a-shorthand-to-make-all-fields-of-a-struct-the-same-type/98961 "2023-05-16T23:20:52Z")

</div>

E.g struct myStruct ::Float64 #apply to all a b c end

---

## [Use a macro inside a function's definition](https://discourse.julialang.org/t/use-a-macro-inside-a-functions-definition/86321)

<div class="topic-metadata">

**Author:** [@Jeroen](https://discourse.julialang.org/u/Jeroen)\
**Replies:** 2\
**Last updated:** [May 12, 2023, 4:30pm UTC](https://discourse.julialang.org/t/use-a-macro-inside-a-functions-definition/86321 "2023-05-12T16:30:37Z")

</div>

Hello, I have a function called f′ and I want to use a macro called @diff inside the function’s definition: macro diff(expression) return :(2x) end f′(x) = @diff x^2 print(f′(10)) Yet once I run this code, this …

---

## [How to limit scope of evaluated variable assignment?](https://discourse.julialang.org/t/how-to-limit-scope-of-evaluated-variable-assignment/98506)

<div class="topic-metadata">

**Author:** [@g2g](https://discourse.julialang.org/u/g2g)\
**Replies:** 3\
**Last updated:** [May 11, 2023, 4:58am UTC](https://discourse.julialang.org/t/how-to-limit-scope-of-evaluated-variable-assignment/98506 "2023-05-11T04:58:06Z")

</div>

As described in this early draft essay, I’m developing a somewhat terse text specification of 3D meshes as an attempt to make 3D modeling more accessible to the blind. The code is probably working good enough for now but…

---

## [Struct and macro - currency pair - ways to specify](https://discourse.julialang.org/t/struct-and-macro-currency-pair-ways-to-specify/97930)

<div class="topic-metadata">

**Author:** [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Replies:** 1\
**Last updated:** [April 26, 2023, 6:53am UTC](https://discourse.julialang.org/t/struct-and-macro-currency-pair-ways-to-specify/97930 "2023-04-26T06:53:15Z")

</div>

I’ve defined a currency pair as struct ccy\_pair; c1::Symbol c2::Symbol end ccy\_pair(x::String) = ccy\_pair(Symbol(x\[1:3\]),Symbol(x\[end-2:end\])) macro p\_str(x) ccy\_pair(x) end So I can write as ccy\_p…

---

## [Non-standard string literals create global-like objects!?](https://discourse.julialang.org/t/non-standard-string-literals-create-global-like-objects/97283)

<div class="topic-metadata">

**Author:** [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Replies:** 6\
**Last updated:** [April 9, 2023, 6:26pm UTC](https://discourse.julialang.org/t/non-standard-string-literals-create-global-like-objects/97283 "2023-04-09T18:26:31Z")

</div>

I am very confused by the way non-standard string literals work when called inside of other functions. See this example with regex (but it happens with any non-standard string literal): r"a" === r"a" # false Regex("a") …

---

## [Expressions with Arbitrary Objects in their \`args\` Arguments](https://discourse.julialang.org/t/expressions-with-arbitrary-objects-in-their-args-arguments/94771)

<div class="topic-metadata">

**Author:** [@uniment](https://discourse.julialang.org/u/uniment)\
**Replies:** 6\
**Last updated:** [April 8, 2023, 12:36pm UTC](https://discourse.julialang.org/t/expressions-with-arbitrary-objects-in-their-args-arguments/94771 "2023-04-08T12:36:21Z")

</div>

I checked Core.Expr, Core.eval, Metaprogramming, and Julia ASTs, and I wasn’t able to find an answer. Is this behavior intended and supported? Let’s build a couple of expressions: julia\> ax1, ax2 = :(\[1:5;\]), \[1:5;\] (:…

---

## [Relocatability of @\_\_DIR\_\_](https://discourse.julialang.org/t/relocatability-of-dir/96700)

<div class="topic-metadata">

**Author:** [@sstroemer](https://discourse.julialang.org/u/sstroemer)\
**Replies:** 2\
**Last updated:** [March 28, 2023, 1:49pm UTC](https://discourse.julialang.org/t/relocatability-of-dir/96700 "2023-03-28T13:49:46Z")

</div>

I am using the DIR macro in a function like: function foo() return joinpath(@\_\_DIR\_\_, "myfile.ext") end If function foo() gets precompiled using PackageCompiler.jl into a sysimage, the @\_\_DIR\_\_ is “hardcoded” into …

---

## [Help writing a timeout macro](https://discourse.julialang.org/t/help-writing-a-timeout-macro/16591)

<div class="topic-metadata">

**Author:** [@lstagner](https://discourse.julialang.org/u/lstagner)\
**Replies:** 12\
**Last updated:** [March 26, 2023, 12:32pm UTC](https://discourse.julialang.org/t/help-writing-a-timeout-macro/16591 "2023-03-26T12:32:16Z")

</div>

I have a function that can either execute quickly or take a very long time. I want to kill the function after a certain amount of time so I can fall back on a more reliable method. I should be able to do this using a mac…

---

## [Macro to choose macros](https://discourse.julialang.org/t/macro-to-choose-macros/96507)

<div class="topic-metadata">

**Author:** [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Replies:** 6\
**Last updated:** [March 24, 2023, 8:35am UTC](https://discourse.julialang.org/t/macro-to-choose-macros/96507 "2023-03-24T08:35:19Z")

</div>

It seems I still don’t understand how macros really work in Julia. (sigh) I want to define a macro @always\_fast which will insert @simd or Threads.@threads depending on the value of Threads.nthreads() for cases like: @…

---

## [How to split a very long string used in a macro?](https://discourse.julialang.org/t/how-to-split-a-very-long-string-used-in-a-macro/96021)

<div class="topic-metadata">

**Author:** [@g2g](https://discourse.julialang.org/u/g2g)\
**Replies:** 4\
**Last updated:** [March 13, 2023, 10:28pm UTC](https://discourse.julialang.org/t/how-to-split-a-very-long-string-used-in-a-macro/96021 "2023-03-13T22:28:39Z")

</div>

To avoid the horizontal scrolling necessary to read a very long unsplit string, any suggestions on how to split the following very long string used in a macro? # mwestr.jl : very long string used by a macro using GLMaki…

---

## [What does \`Base.@ccallable\` do?](https://discourse.julialang.org/t/what-does-base-ccallable-do/71194)

<div class="topic-metadata">

**Author:** [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Replies:** 2\
**Last updated:** [February 9, 2023, 7:06pm UTC](https://discourse.julialang.org/t/what-does-base-ccallable-do/71194 "2023-02-09T19:06:12Z")

</div>

It’s mentioned in the docs for PackageCompiler (Apps · PackageCompiler) but is not itself documented.

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

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