# \#macros

**URL:** https://discourse.julialang.org/tag/macros/67.md?page=4

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

**Page:** 5

---

## [Is it possible to avoid eval in this code?](https://discourse.julialang.org/t/is-it-possible-to-avoid-eval-in-this-code/93975)

<div class="topic-metadata">

**Author:** [@Alexander-Barth](https://discourse.julialang.org/u/Alexander-Barth)\
**Replies:** 13\
**Last updated:** [February 5, 2023, 8:03pm UTC](https://discourse.julialang.org/t/is-it-possible-to-avoid-eval-in-this-code/93975 "2023-02-05T20:03:50Z")

</div>

I would like to write a macro which subsets a collection of variables (with shared dimensions). My use case can be simplified to a dictionary of vectors where each vector has the same length, for example: ds = Dict(:a =\>…

---

## [Order in which macros are processed](https://discourse.julialang.org/t/order-in-which-macros-are-processed/93530)

<div class="topic-metadata">

**Author:** [@brianguenter](https://discourse.julialang.org/u/brianguenter)\
**Replies:** 0\
**Last updated:** [January 25, 2023, 7:03pm UTC](https://discourse.julialang.org/t/order-in-which-macros-are-processed/93530 "2023-01-25T19:03:45Z")

</div>

Given the following code in a file called “TestMacro.jl”: module TestMacro import Base struct Glob end export Glob macro define() :(Base.:\*(a::Glob, b::Glob) = "here") end @define Base.:\*(a::Glob, b::Glob) = "th…

---

## [Module scoping and macro expansion](https://discourse.julialang.org/t/module-scoping-and-macro-expansion/92914)

<div class="topic-metadata">

**Author:** [@veddox](https://discourse.julialang.org/u/veddox)\
**Replies:** 4\
**Last updated:** [January 21, 2023, 11:10am UTC](https://discourse.julialang.org/t/module-scoping-and-macro-expansion/92914 "2023-01-21T11:10:56Z")

</div>

So I’ve been getting really confused with the interaction between nested macro expansion and module scoping… In my module MyModel I have a set of macros with which I implement a simple DSL. Things work fine in this simp…

---

## [How to create a non-standard string literal with parameters in Julia?](https://discourse.julialang.org/t/how-to-create-a-non-standard-string-literal-with-parameters-in-julia/93037)

<div class="topic-metadata">

**Author:** [@Flavio\_Barros](https://discourse.julialang.org/u/Flavio_Barros)\
**Replies:** 8\
**Last updated:** [January 16, 2023, 7:29pm UTC](https://discourse.julialang.org/t/how-to-create-a-non-standard-string-literal-with-parameters-in-julia/93037 "2023-01-16T19:29:59Z")

</div>

Can someone answer my question o Stack Overflow: How to create a non-standard string literal with parameters in Julia?

---

## [How to tell if code is being run in global scope?](https://discourse.julialang.org/t/how-to-tell-if-code-is-being-run-in-global-scope/92982)

<div class="topic-metadata">

**Author:** [@Jollywatt](https://discourse.julialang.org/u/Jollywatt)\
**Replies:** 1\
**Last updated:** [January 15, 2023, 6:13am UTC](https://discourse.julialang.org/t/how-to-tell-if-code-is-being-run-in-global-scope/92982 "2023-01-15T06:13:25Z")

</div>

How do you tell if code is being evaluated at top-level, or directly into the REPL? I have a macro that defines some variables in the current scope. For example: macro defs(n) assignments = \[:($(esc(Symbol("x$i"))) = …

---

## [Should a macro throw an error, or return expression that throws an error?](https://discourse.julialang.org/t/should-a-macro-throw-an-error-or-return-expression-that-throws-an-error/92720)

<div class="topic-metadata">

**Author:** [@bbejanov](https://discourse.julialang.org/u/bbejanov)\
**Replies:** 6\
**Last updated:** [January 10, 2023, 3:55pm UTC](https://discourse.julialang.org/t/should-a-macro-throw-an-error-or-return-expression-that-throws-an-error/92720 "2023-01-10T15:55:03Z")

</div>

I have a macro that in some circumstances must throw an error. Is there a recommended way of doing that? For example, I can throw the error directly from the macro, or return code that throws the error. macro A() i…

---

## [Meta-metaprogramming practical examples](https://discourse.julialang.org/t/meta-metaprogramming-practical-examples/92535)

<div class="topic-metadata">

**Author:** [@xlxs4](https://discourse.julialang.org/u/xlxs4)\
**Replies:** 4\
**Last updated:** [January 6, 2023, 3:23am UTC](https://discourse.julialang.org/t/meta-metaprogramming-practical-examples/92535 "2023-01-06T03:23:47Z")

</div>

From Tom Kwongs’ excellent Hands-on Design Patterns and Best Practices with Julia: It is possible to have a quoted expression that contains another quoted expression. \[…\] :( :( x = 1 ) ) |\> dump \[…\] It can be usefu…

---

## [Why can't I combine a macro call with a comparison operator?](https://discourse.julialang.org/t/why-cant-i-combine-a-macro-call-with-a-comparison-operator/92493)

<div class="topic-metadata">

**Author:** [@veddox](https://discourse.julialang.org/u/veddox)\
**Replies:** 5\
**Last updated:** [January 5, 2023, 6:13am UTC](https://discourse.julialang.org/t/why-cant-i-combine-a-macro-call-with-a-comparison-operator/92493 "2023-01-05T06:13:37Z")

</div>

To save myself a bit of typing, I have created a wrapper macro around a function in my model, to be used in a specific context: macro landcover() :(landcover($(esc(:pos)), $(esc(:model)))) end I want to be able to …

---

## [Can't we create a macro named "?"](https://discourse.julialang.org/t/cant-we-create-a-macro-named/92027)

<div class="topic-metadata">

**Author:** [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Replies:** 2\
**Last updated:** [December 22, 2022, 11:41pm UTC](https://discourse.julialang.org/t/cant-we-create-a-macro-named/92027 "2022-12-22T23:41:47Z")

</div>

julia\> @? meca ┌ Error: Failed to revise C:\\Users\\joaqu\\.julia\\dev\\GMT\\src\\common\_options.jl │ exception = │ LoadError: "invalid identifier name \\"?\\"" But we have the broadcast macro @. (which I can’t find the def…

---

## [Access docstring within a macro](https://discourse.julialang.org/t/access-docstring-within-a-macro/90362)

<div class="topic-metadata">

**Author:** [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Replies:** 5\
**Last updated:** [December 19, 2022, 9:26pm UTC](https://discourse.julialang.org/t/access-docstring-within-a-macro/90362 "2022-12-19T21:26:24Z")

</div>

Hello Is there any way to access the docstring accompanying a macro at call place? It would be something like this: macro MyMacro(arg) # Do something with my docs # It does not mean that I want to append the outer doc …

---

## [Using a (computed) function in DataFrames with multiple arguments](https://discourse.julialang.org/t/using-a-computed-function-in-dataframes-with-multiple-arguments/91833)

<div class="topic-metadata">

**Author:** [@mreichMPI-BGC](https://discourse.julialang.org/u/mreichMPI-BGC)\
**Replies:** 11\
**Last updated:** [December 19, 2022, 10:26am UTC](https://discourse.julialang.org/t/using-a-computed-function-in-dataframes-with-multiple-arguments/91833 "2022-12-19T10:26:31Z")

</div>

In Julia I can do: meanratio(x,y) = mean(x) / mean(y) function calc(fun, vbl) fun(vbl...) end julia\> x1=rand(10); x2=rand(10);calc(meanratio, (x1, x2)) 0.8271083961061552 I want fun to be flexible in term of how…

---

## [A macro for pretty geometric algebra equations?](https://discourse.julialang.org/t/a-macro-for-pretty-geometric-algebra-equations/91664)

<div class="topic-metadata">

**Author:** [@g2g](https://discourse.julialang.org/u/g2g)\
**Replies:** 15\
**Last updated:** [December 17, 2022, 1:21pm UTC](https://discourse.julialang.org/t/a-macro-for-pretty-geometric-algebra-equations/91664 "2022-12-17T13:21:32Z")

</div>

The following slide is from Steven De Keninck’s presentation at SIBGAPI, an international conference annually promoted by the Special Interest Group on Computer Graphics and Image Processing (CEGRAPI) of the Brazilian Co…

---

## [A fast reference implementation of a pretty projective geometric algebra](https://discourse.julialang.org/t/a-fast-reference-implementation-of-a-pretty-projective-geometric-algebra/91451)

<div class="topic-metadata">

**Author:** [@g2g](https://discourse.julialang.org/u/g2g)\
**Replies:** 18\
**Last updated:** [December 14, 2022, 5:52pm UTC](https://discourse.julialang.org/t/a-fast-reference-implementation-of-a-pretty-projective-geometric-algebra/91451 "2022-12-14T17:52:25Z")

</div>

As shown at the end of this post, I recently ported pga3d.cpp (the bivector.net C++ reference implementation of projective geometric algebra) to Julia because bivector.net does not currently offer a Julia reference imple…

---

## [Why \`@nospecialize(ex)\` in macros?](https://discourse.julialang.org/t/why-nospecialize-ex-in-macros/91589)

<div class="topic-metadata">

**Author:** [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Replies:** 2\
**Last updated:** [December 14, 2022, 8:28am UTC](https://discourse.julialang.org/t/why-nospecialize-ex-in-macros/91589 "2022-12-14T08:28:04Z")

</div>

Why do Julia experts use @nospecialize(ex) in functions used by macros? E.g. here: Typed globals: macro to define multiple, without manual type annotations - #2 by simeonschaub I’d say the only types the argument could…

---

## [Typed globals: macro to define multiple, without manual type annotations](https://discourse.julialang.org/t/typed-globals-macro-to-define-multiple-without-manual-type-annotations/90818)

<div class="topic-metadata">

**Author:** [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Replies:** 9\
**Last updated:** [December 13, 2022, 12:01pm UTC](https://discourse.julialang.org/t/typed-globals-macro-to-define-multiple-without-manual-type-annotations/90818 "2022-12-13T12:01:00Z")

</div>

I vaguely remember seeing a macro somewhere to define typed globals without having to give the type explicitly, something like @typed begin a = 2 b = 2.8 ⋮ end which would expand to a::Int = 2 b::Float64 = 2.8 ⋮…

---

## [Question about macro and how to resolve/esc variables/expressions](https://discourse.julialang.org/t/question-about-macro-and-how-to-resolve-esc-variables-expressions/91309)

<div class="topic-metadata">

**Author:** [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Replies:** 1\
**Last updated:** [December 6, 2022, 8:19pm UTC](https://discourse.julialang.org/t/question-about-macro-and-how-to-resolve-esc-variables-expressions/91309 "2022-12-06T20:19:08Z")

</div>

Hi all :slight\_smile: Here is a little example code. I’m interested in the local var \_error\_msg : local \_error\_msg = $(esc(sprint(showerror, error, catch\_backtrace()))) So what I want is the error and catch\_backtrace …

---

## [Macro question (create variables)](https://discourse.julialang.org/t/macro-question-create-variables/90160)

<div class="topic-metadata">

**Author:** [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Replies:** 4\
**Last updated:** [November 12, 2022, 9:09pm UTC](https://discourse.julialang.org/t/macro-question-create-variables/90160 "2022-11-12T21:09:44Z")

</div>

Wanted: a macro @moo a b which is in effect equivalent to a = foo("a") b = foo("b") and actually I got it: foo(x) = "$x-$x" macro moo(args...) for x in args xs = string(x) @eval eval(Meta.parse…

---

## [Why use metaprogramming?](https://discourse.julialang.org/t/why-use-metaprogramming/89626)

<div class="topic-metadata">

**Author:** [@1153dent](https://discourse.julialang.org/u/1153dent)\
**Replies:** 7\
**Last updated:** [November 5, 2022, 7:21pm UTC](https://discourse.julialang.org/t/why-use-metaprogramming/89626 "2022-11-05T19:21:12Z")

</div>

Hi all I am planning on doing the following in PlutoHooks.jl stream structured IPM’s into a ZMQ pipe have the IPM’s processed in a Pluto notebook cell using Plutohooks.jl the IPM data will be used to update ( in real …

---

## [Prevent escaping for single symbol inside macro body](https://discourse.julialang.org/t/prevent-escaping-for-single-symbol-inside-macro-body/89609)

<div class="topic-metadata">

**Author:** [@disberd](https://discourse.julialang.org/u/disberd)\
**Replies:** 0\
**Last updated:** [November 1, 2022, 10:37am UTC](https://discourse.julialang.org/t/prevent-escaping-for-single-symbol-inside-macro-body/89609 "2022-11-01T10:37:12Z")

</div>

Is there a function or any other way to prevent escaping of a specific symbol when wrapping the expr returned by a macro with esc()? I sometime want to have a few symbols within the macro body to be unescaped and it wou…

---

## [Using @eval in a function](https://discourse.julialang.org/t/using-eval-in-a-function/89488)

<div class="topic-metadata">

**Author:** [@vitaminace33](https://discourse.julialang.org/u/vitaminace33)\
**Replies:** 17\
**Last updated:** [October 29, 2022, 10:40pm UTC](https://discourse.julialang.org/t/using-eval-in-a-function/89488 "2022-10-29T22:40:23Z")

</div>

Hi! I am trying to extend the methods of a collection of functions programmatically with no partial success. foo(x::Real) = x for f in (:foo,) @eval ($f)(x::Complex) = x' end function fun() foo(x::Real) = x …

---

## [Why are macros re-pre-compiled](https://discourse.julialang.org/t/why-are-macros-re-pre-compiled/89319)

<div class="topic-metadata">

**Author:** [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Replies:** 4\
**Last updated:** [October 27, 2022, 3:39pm UTC](https://discourse.julialang.org/t/why-are-macros-re-pre-compiled/89319 "2022-10-27T15:39:26Z")

</div>

I am trying to improve load time of a package of mine. I found that a lot of this time was spent type-inferring macro-related functions, for example: Through using @match (Match.jl): Match.gen\_match\_expr(::String, :…

---

## [Passing keyword arguments to nested functions | or how to use macros to define a function whose kwargs are those of their inner function calls](https://discourse.julialang.org/t/passing-keyword-arguments-to-nested-functions-or-how-to-use-macros-to-define-a-function-whose-kwargs-are-those-of-their-inner-function-calls/88671)

<div class="topic-metadata">

**Author:** [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Replies:** 0\
**Last updated:** [October 13, 2022, 2:48pm UTC](https://discourse.julialang.org/t/passing-keyword-arguments-to-nested-functions-or-how-to-use-macros-to-define-a-function-whose-kwargs-are-those-of-their-inner-function-calls/88671 "2022-10-13T14:48:41Z")

</div>

Important: I am trying to figure this out to compare to a Python approach, which I mention in the second part of the question. Suppose I have two functions, foo and bar, whose keyword argument I don’t know or care. Sup…

---

## [Documentation for how macros handle keyword arguments](https://discourse.julialang.org/t/documentation-for-how-macros-handle-keyword-arguments/88009)

<div class="topic-metadata">

**Author:** [@goerz](https://discourse.julialang.org/u/goerz)\
**Replies:** 2\
**Last updated:** [October 1, 2022, 2:51am UTC](https://discourse.julialang.org/t/documentation-for-how-macros-handle-keyword-arguments/88009 "2022-10-01T02:51:41Z")

</div>

Macros seem to have a pretty weird way of dealing with keyword arguments, especially keyword arguments that are passed with ;. It seems arguments after ; are moved to the front of the macro call, except if the macro is c…

---

## [Create Formulas with Macros](https://discourse.julialang.org/t/create-formulas-with-macros/87951)

<div class="topic-metadata">

**Author:** [@Matthew](https://discourse.julialang.org/u/Matthew)\
**Replies:** 4\
**Last updated:** [September 29, 2022, 6:08pm UTC](https://discourse.julialang.org/t/create-formulas-with-macros/87951 "2022-09-29T18:08:55Z")

</div>

Hi all, I’m practicing working with macros and ran into an idea that I want to attempt to implement. Essentially, I want to create a macro @form that takes a formula expression. The expression would then be applied to a…

---

## [Macro accessing fields of struct at another module](https://discourse.julialang.org/t/macro-accessing-fields-of-struct-at-another-module/87893)

<div class="topic-metadata">

**Author:** [@Ronneesley](https://discourse.julialang.org/u/Ronneesley)\
**Replies:** 1\
**Last updated:** [September 28, 2022, 2:48pm UTC](https://discourse.julialang.org/t/macro-accessing-fields-of-struct-at-another-module/87893 "2022-09-28T14:48:15Z")

</div>

Hello, I’ve a module with a macro: julia\> module MyModule export @mymacro macro mymacro(T) return fieldnames( eval(T) ) end end When I’m at another module or at Julia’s shell I can’t use my macro: j…

---

## [Higher order function as macro](https://discourse.julialang.org/t/higher-order-function-as-macro/87867)

<div class="topic-metadata">

**Author:** [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Replies:** 1\
**Last updated:** [September 27, 2022, 11:07am UTC](https://discourse.julialang.org/t/higher-order-function-as-macro/87867 "2022-09-27T11:07:37Z")

</div>

I am trying provide a macro form of a higher order function, but the automatic macro hygiene means that the name of the resulting function gets mangled. Here is my attempt so far: # The higher order function I want to…

---

## [How to write a macro to export all instances of an enum type](https://discourse.julialang.org/t/how-to-write-a-macro-to-export-all-instances-of-an-enum-type/73137)

<div class="topic-metadata">

**Author:** [@paalon](https://discourse.julialang.org/u/paalon)\
**Replies:** 2\
**Last updated:** [September 22, 2022, 8:45am UTC](https://discourse.julialang.org/t/how-to-write-a-macro-to-export-all-instances-of-an-enum-type/73137 "2022-09-22T08:45:58Z")

</div>

A module has an enum type A and I want to export all its instances instances(A). I want to write a macro @export\_instances like module MyModule export A @enum A a b c @export\_instances A # \`export a, b, c\` end # module…

---

## [Metaprogramming: macro creating types](https://discourse.julialang.org/t/metaprogramming-macro-creating-types/87439)

<div class="topic-metadata">

**Author:** [@davidavdav](https://discourse.julialang.org/u/davidavdav)\
**Replies:** 2\
**Last updated:** [September 19, 2022, 8:52am UTC](https://discourse.julialang.org/t/metaprogramming-macro-creating-types/87439 "2022-09-19T08:52:12Z")

</div>

Hi, I’ve been struggling with making a macro that will define a new wrapper type for me, with some minimal functions to make the wrapper type function more/less the same as the type it waps. For now have the minimal wr…

---

## [Avoiding eval() - struggling with correct macro syntax](https://discourse.julialang.org/t/avoiding-eval-struggling-with-correct-macro-syntax/70665)

<div class="topic-metadata">

**Author:** [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Replies:** 3\
**Last updated:** [September 12, 2022, 10:05pm UTC](https://discourse.julialang.org/t/avoiding-eval-struggling-with-correct-macro-syntax/70665 "2022-09-12T22:05:25Z")

</div>

I’m again struggling with correct macro syntax. I have a macro that is doing it’s job as required but relies on eval(), which I understand is not ideal. The macro should insert fields of an existing struct into a new st…

---

## [Namedtuple destructuring in macro fails roughly due to macro hygiene (since gensym goes eagerly)](https://discourse.julialang.org/t/namedtuple-destructuring-in-macro-fails-roughly-due-to-macro-hygiene-since-gensym-goes-eagerly/86172)

<div class="topic-metadata">

**Author:** [@o314](https://discourse.julialang.org/u/o314)\
**Replies:** 2\
**Last updated:** [September 8, 2022, 9:14am UTC](https://discourse.julialang.org/t/namedtuple-destructuring-in-macro-fails-roughly-due-to-macro-hygiene-since-gensym-goes-eagerly/86172 "2022-09-08T09:14:35Z")

</div>

Here is the situation f(x) = (; a=2x, b=x\*x) macro ma(x) quote f($x) end end macro mb(x) quote nt = f($x) (; nt.a, nt.b) end end macro mc(x) quote (; a,b) = f($x) …

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

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