# \#metaprogramming

**URL:** https://discourse.julialang.org/tag/metaprogramming/68.md?match_all_tags=true&page=6&tags%5B%5D=metaprogramming

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

**Page:** 7

---

## [Unit testing for macros?](https://discourse.julialang.org/t/unit-testing-for-macros/66230)

<div class="topic-metadata">

**Author:** [@tbenst](https://discourse.julialang.org/u/tbenst)\
**Replies:** 3\
**Last updated:** [August 12, 2021, 1:50pm UTC](https://discourse.julialang.org/t/unit-testing-for-macros/66230 "2021-08-12T13:50:50Z")

</div>

Suppose I write some complicated macro: "Return exact same expression." macro flawed\_identity(ex) @assert ex.head != :(=) "oops we don't support head of :=" ex end and I write some tests: @testset "identity" b…

---

## [Transforming arbitrary Julia code to lazy evaluation](https://discourse.julialang.org/t/transforming-arbitrary-julia-code-to-lazy-evaluation/66239)

<div class="topic-metadata">

**Author:** [@tbenst](https://discourse.julialang.org/u/tbenst)\
**Replies:** 4\
**Last updated:** [August 12, 2021, 3:51am UTC](https://discourse.julialang.org/t/transforming-arbitrary-julia-code-to-lazy-evaluation/66239 "2021-08-12T03:51:01Z")

</div>

I’ve been experimenting with methods for transforming arbitrary Julia code for lazy evaluation (repo at https://github.com/tbenst/Thunks.jl). Here’s an example of what I’d like to be able to do in a fraction of a second: …

---

## [Passing macro as function argument?](https://discourse.julialang.org/t/passing-macro-as-function-argument/65898)

<div class="topic-metadata">

**Author:** [@tbenst](https://discourse.julialang.org/u/tbenst)\
**Replies:** 5\
**Last updated:** [August 5, 2021, 6:07pm UTC](https://discourse.julialang.org/t/passing-macro-as-function-argument/65898 "2021-08-05T18:07:06Z")

</div>

I’m trying to benchmark various parallelization approaches, and realized I’m not sure how to change which macro is called…? Is this possible? function count\_threads(macro\_name=Threads.@threads) n = Threads.nthreads(…

---

## [Metaprogramming with Structure: Converting Dictionary to Structure](https://discourse.julialang.org/t/metaprogramming-with-structure-converting-dictionary-to-structure/63861)

<div class="topic-metadata">

**Author:** [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)\
**Replies:** 14\
**Last updated:** [July 11, 2021, 10:40pm UTC](https://discourse.julialang.org/t/metaprogramming-with-structure-converting-dictionary-to-structure/63861 "2021-07-11T22:40:46Z")

</div>

Dear Julian colleagues, I am reading the options of my model from TOML file into Structure. With the following steps: The TOML writes into a Dict: Than I am writing a code to parse Dict into Structure The code works…

---

## [Combining @eval and @testset macros](https://discourse.julialang.org/t/combining-eval-and-testset-macros/64250)

<div class="topic-metadata">

**Author:** [@ajhynes7](https://discourse.julialang.org/u/ajhynes7)\
**Replies:** 2\
**Last updated:** [July 8, 2021, 4:40pm UTC](https://discourse.julialang.org/t/combining-eval-and-testset-macros/64250 "2021-07-08T16:40:14Z")

</div>

I’m trying to test two types that have similar properties. I want to perform the same set of tests on instances of the two types. If I was using Python, I would use the @pytest.mark.parametrize decorator to accomplish th…

---

## [Replacing eval(Meta.parse(...)) with macro](https://discourse.julialang.org/t/replacing-eval-meta-parse-with-macro/62788)

<div class="topic-metadata">

**Author:** [@MartaVanin](https://discourse.julialang.org/u/MartaVanin)\
**Replies:** 4\
**Last updated:** [June 12, 2021, 1:31pm UTC](https://discourse.julialang.org/t/replacing-eval-meta-parse-with-macro/62788 "2021-06-12T13:31:41Z")

</div>

Hello, I want to push a number of anonymous functions to an array. Each function is largely the same but in some cases some addends are scalars instead of variables. Say that there are three different cases, like: case…

---

## [Switch off chunks of code](https://discourse.julialang.org/t/switch-off-chunks-of-code/62247)

<div class="topic-metadata">

**Author:** [@Adriel](https://discourse.julialang.org/u/Adriel)\
**Replies:** 3\
**Last updated:** [June 2, 2021, 5:02am UTC](https://discourse.julialang.org/t/switch-off-chunks-of-code/62247 "2021-06-02T05:02:33Z")

</div>

I’m wondering if there’s a way to completely remove sections of debug/analytics code via some switch, similar to #define in C. Currently I’m just doing this. function f(x, analyse=false) # compute output if anal…

---

## [How to "broadcast" macro interpolation](https://discourse.julialang.org/t/how-to-broadcast-macro-interpolation/61641)

<div class="topic-metadata">

**Author:** [@melonedo](https://discourse.julialang.org/u/melonedo)\
**Replies:** 4\
**Last updated:** [May 23, 2021, 2:32am UTC](https://discourse.julialang.org/t/how-to-broadcast-macro-interpolation/61641 "2021-05-23T02:32:04Z")

</div>

In Julia, building a simple expression is as easy as inserting a few $s. For example if I write a macro that zeros a variable by setting it to literal 0: macro zeroit(x::Symbol) :($(esc(x)) = 0) end But if I want a…

---

## [Not-so-unusual Macro issues](https://discourse.julialang.org/t/not-so-unusual-macro-issues/61570)

<div class="topic-metadata">

**Author:** [@harris-chris](https://discourse.julialang.org/u/harris-chris)\
**Replies:** 2\
**Last updated:** [May 22, 2021, 1:36am UTC](https://discourse.julialang.org/t/not-so-unusual-macro-issues/61570 "2021-05-22T01:36:41Z")

</div>

Hello all. I’m having some macro-related issues. I’d like, if it’s even possible, to store the Expression of a function defined by the user, so I can write it to a file and use it elsewhere. I imagined that this would be…

---

## [Debugging Threads.@spawn applied to Expr data](https://discourse.julialang.org/t/debugging-threads-spawn-applied-to-expr-data/61359)

<div class="topic-metadata">

**Author:** [@ericsfraga](https://discourse.julialang.org/u/ericsfraga)\
**Replies:** 2\
**Last updated:** [May 19, 2021, 10:39am UTC](https://discourse.julialang.org/t/debugging-threads-spawn-applied-to-expr-data/61359 "2021-05-19T10:39:10Z")

</div>

I am using meta-programming combined with multi-threading. I am trying to @spawn an Expr expression which is evaluated, i.e. e = quote \[...\] end tmp = Threads.@spawn eval(e) If I @show tmp, sometimes it says Task (runn…

---

## [\[Julia usage / metaprogramming\] How to evaluate a module's function with function's name as a string?](https://discourse.julialang.org/t/julia-usage-metaprogramming-how-to-evaluate-a-modules-function-with-functions-name-as-a-string/60031)

<div class="topic-metadata">

**Author:** [@erwanlecarpentier](https://discourse.julialang.org/u/erwanlecarpentier)\
**Replies:** 10\
**Last updated:** [April 26, 2021, 2:17pm UTC](https://discourse.julialang.org/t/julia-usage-metaprogramming-how-to-evaluate-a-modules-function-with-functions-name-as-a-string/60031 "2021-04-26T14:17:25Z")

</div>

Hi! Inside a function, I would like to evaluate a module’s function based on two inputs: 1) the module itself and 2) the function’s name as a string. Here is the code: module A function foo() println("This is A.foo…

---

## [Generate function to build a static array from vector of vector](https://discourse.julialang.org/t/generate-function-to-build-a-static-array-from-vector-of-vector/59862)

<div class="topic-metadata">

**Author:** [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Replies:** 5\
**Last updated:** [April 26, 2021, 9:17am UTC](https://discourse.julialang.org/t/generate-function-to-build-a-static-array-from-vector-of-vector/59862 "2021-04-26T09:17:10Z")

</div>

I have a Vector of Vectors (actually Vector of SVectors, but I do not believe this is relevant to the question.) All vectors, inner and outer, have length N. I wish to generate an SMatrix from it in a typestable mann…

---

## [\[ANN\] Expronicon.jl - meta programming made easy](https://discourse.julialang.org/t/ann-expronicon-jl-meta-programming-made-easy/57765)

<div class="topic-metadata">

**Author:** [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Replies:** 2\
**Last updated:** [April 6, 2021, 5:58am UTC](https://discourse.julialang.org/t/ann-expronicon-jl-meta-programming-made-easy/57765 "2021-04-06T05:58:41Z")

</div>

I created a new package Expronicon.jl to wrap up some meta-programming tools on Julia’s Expr I wrote in the past year using MLStyle The goal of Expronicon is to serve as a stdlib for MLStyle for meta programming Julia Ex…

---

## [\[ANN\] ExproniconLite.jl - the MLStyle generated dependency free package for low latency](https://discourse.julialang.org/t/ann-exproniconlite-jl-the-mlstyle-generated-dependency-free-package-for-low-latency/59247)

<div class="topic-metadata">

**Author:** [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Replies:** 0\
**Last updated:** [April 14, 2021, 1:23am UTC](https://discourse.julialang.org/t/ann-exproniconlite-jl-the-mlstyle-generated-dependency-free-package-for-low-latency/59247 "2021-04-14T01:23:10Z")

</div>

I’m excited to announce a new package ExproniconLite that is generated by the Expronicon package I announced previously: unfortunately, depending on MLStyle itself will cause a small loading latency which can be unple…

---

## [Generating function from existing function body + custom return statement](https://discourse.julialang.org/t/generating-function-from-existing-function-body-custom-return-statement/59212)

<div class="topic-metadata">

**Author:** [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Replies:** 3\
**Last updated:** [April 13, 2021, 8:18pm UTC](https://discourse.julialang.org/t/generating-function-from-existing-function-body-custom-return-statement/59212 "2021-04-13T20:18:34Z")

</div>

Hello, I’m exploring an idea which may look funny. Please tell me if you thinks it’s impossible or useless. My goal I’d like to generate automatically (with metaprogramming I guess) functions which body and signature c…

---

## [Turning Expressions into Functions](https://discourse.julialang.org/t/turning-expressions-into-functions/49558)

<div class="topic-metadata">

**Author:** [@1ozturkbe](https://discourse.julialang.org/u/1ozturkbe)\
**Replies:** 17\
**Last updated:** [April 12, 2021, 12:41pm UTC](https://discourse.julialang.org/t/turning-expressions-into-functions/49558 "2021-04-12T12:41:16Z")

</div>

I have the following Expression: ex = :(sum(x\[i\] for i=1:4)- y\[1\] \* y\[2\] + z) which I would like to evaluate over some data x, y, z. I have tried doing direct substitution through symbolic manipulations, but it is abys…

---

## [Getting CartesianIndices to propagate](https://discourse.julialang.org/t/getting-cartesianindices-to-propagate/57315)

<div class="topic-metadata">

**Author:** [@RainerHeintzmann](https://discourse.julialang.org/u/RainerHeintzmann)\
**Replies:** 8\
**Last updated:** [March 20, 2021, 11:43am UTC](https://discourse.julialang.org/t/getting-cartesianindices-to-propagate/57315 "2021-03-20T11:43:33Z")

</div>

Julia (here 1.6RC1 on Windows 10) has a number of mechanism to calculate with positions inside an array such as ranges like (1:10) or generators or via CartesianIndices(x). Yet they all seem to not automatically propagat…

---

## [Macro for generating struct](https://discourse.julialang.org/t/macro-for-generating-struct/57479)

<div class="topic-metadata">

**Author:** [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Replies:** 4\
**Last updated:** [March 19, 2021, 5:40am UTC](https://discourse.julialang.org/t/macro-for-generating-struct/57479 "2021-03-19T05:40:00Z")

</div>

I have written a macro that generates a new struct. It all works as intended when I test the macro from the REPL or from a script. Then of course, I want to call the macro in a function that does some cooking and retur…

---

## [Metaprogramming in Pluto notebook](https://discourse.julialang.org/t/metaprogramming-in-pluto-notebook/56400)

<div class="topic-metadata">

**Author:** [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Replies:** 1\
**Last updated:** [March 3, 2021, 11:56am UTC](https://discourse.julialang.org/t/metaprogramming-in-pluto-notebook/56400 "2021-03-03T11:56:03Z")

</div>

I currently have some pretty repeditive code to create 50 textfields in a pluto notebook. I do however feel like the same could be acomplished with some smart metaprogramming. However, my naive fumbling around with metap…

---

## [On the performance of function calls that depends on a variable](https://discourse.julialang.org/t/on-the-performance-of-function-calls-that-depends-on-a-variable/55573)

<div class="topic-metadata">

**Author:** [@moudbis](https://discourse.julialang.org/u/moudbis)\
**Replies:** 12\
**Last updated:** [February 24, 2021, 2:07pm UTC](https://discourse.julialang.org/t/on-the-performance-of-function-calls-that-depends-on-a-variable/55573 "2021-02-24T14:07:39Z")

</div>

Hello everyone, Sorry, it’s going to be a bit long. I’m trying to reach performance in Julia for function calls that depends on a variable. It means given a line in my code, I can’t tell which function will be run in a…

---

## [Local scope in macro](https://discourse.julialang.org/t/local-scope-in-macro/55705)

<div class="topic-metadata">

**Author:** [@colinclout12](https://discourse.julialang.org/u/colinclout12)\
**Replies:** 13\
**Last updated:** [February 21, 2021, 10:47pm UTC](https://discourse.julialang.org/t/local-scope-in-macro/55705 "2021-02-21T22:47:32Z")

</div>

I’m trying to figure out how to make variables defined in a let expression be visible in a hygienic macro which is evaluated in that let expression. macro insertx(x) x end let y = 5 @insertx(y) end \>UndefVarError:…

---

## [Prevent interpolation when Meta.parse a String to Expr](https://discourse.julialang.org/t/prevent-interpolation-when-meta-parse-a-string-to-expr/55242)

<div class="topic-metadata">

**Author:** [@Lian\_Yunlong](https://discourse.julialang.org/u/Lian_Yunlong)\
**Replies:** 9\
**Last updated:** [February 15, 2021, 11:04pm UTC](https://discourse.julialang.org/t/prevent-interpolation-when-meta-parse-a-string-to-expr/55242 "2021-02-15T23:04:15Z")

</div>

Hello everyone, if I want to parse the following string to expression s = "Z = A + 1" where A represents a variable defined elsewhere in the code, then how can I prevent interpolation when I pass this string to Meta.p…

---

## [Eval scoping in macros, or removing eval completely](https://discourse.julialang.org/t/eval-scoping-in-macros-or-removing-eval-completely/54602)

<div class="topic-metadata">

**Author:** [@Libbum](https://discourse.julialang.org/u/Libbum)\
**Replies:** 7\
**Last updated:** [February 5, 2021, 11:42am UTC](https://discourse.julialang.org/t/eval-scoping-in-macros-or-removing-eval-completely/54602 "2021-02-05T11:42:46Z")

</div>

Hi all, Took a stroll into marco coding and got totally lost. Trying to solve this issue. Breakdown: We have a simple enough macro helper @agent, which generates a few different types of struct depending on input. @a…

---

## [Macro for partially quoting (or partially evaluating) an expression](https://discourse.julialang.org/t/macro-for-partially-quoting-or-partially-evaluating-an-expression/53519)

<div class="topic-metadata">

**Author:** [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Replies:** 5\
**Last updated:** [January 19, 2021, 1:35am UTC](https://discourse.julialang.org/t/macro-for-partially-quoting-or-partially-evaluating-an-expression/53519 "2021-01-19T01:35:08Z")

</div>

I would like to quote expressions, but evaluate some Symbols (which ones? see below). It should be possible to accomplish this using macros. I’ve tried to give an idea below of what I’m after: """ e.g. \`\`\`julia julia\> …

---

## [Macro to create variables from arrays of names and values](https://discourse.julialang.org/t/macro-to-create-variables-from-arrays-of-names-and-values/53349)

<div class="topic-metadata">

**Author:** [@thorek1](https://discourse.julialang.org/u/thorek1)\
**Replies:** 14\
**Last updated:** [January 15, 2021, 2:23pm UTC](https://discourse.julialang.org/t/macro-to-create-variables-from-arrays-of-names-and-values/53349 "2021-01-15T14:23:53Z")

</div>

Hi, I am stuck writing a macro to create variables from an array of names (as strings) and an array of values to be assigned. So this is what I start with: Names = \["R", "k", "y"\] Values = \[1.01, 4.7, 1.3\] what I wou…

---

## [Interpolating a Dict with Exprs inside a quoted expression](https://discourse.julialang.org/t/interpolating-a-dict-with-exprs-inside-a-quoted-expression/53265)

<div class="topic-metadata">

**Author:** [@MasFlam](https://discourse.julialang.org/u/MasFlam)\
**Replies:** 5\
**Last updated:** [January 13, 2021, 8:25pm UTC](https://discourse.julialang.org/t/interpolating-a-dict-with-exprs-inside-a-quoted-expression/53265 "2021-01-13T20:25:14Z")

</div>

I am coding some macros and in one of them I end up with a Dict{Symbol, Tuple{Symbol, Expr}}, where the Exprs are all supposed to be interpolated into the expression returned by the macro. What I am doing right now is th…

---

## [@nloops with type parameter N](https://discourse.julialang.org/t/nloops-with-type-parameter-n/52733)

<div class="topic-metadata">

**Author:** [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Replies:** 4\
**Last updated:** [January 4, 2021, 12:19pm UTC](https://discourse.julialang.org/t/nloops-with-type-parameter-n/52733 "2021-01-04T12:19:47Z")

</div>

Hey, I was trying to create a function body which contains @nloops N i A. However, the N depends on the dimension of the array. Following MWE: A = randn((2,2,2)) function foo(A::AbstractArray{T, N}) where {T, N} …

---

## [Trouble with macro expression escaping](https://discourse.julialang.org/t/trouble-with-macro-expression-escaping/52312)

<div class="topic-metadata">

**Author:** [@Volker\_Weissmann](https://discourse.julialang.org/u/Volker_Weissmann)\
**Replies:** 3\
**Last updated:** [December 24, 2020, 4:32pm UTC](https://discourse.julialang.org/t/trouble-with-macro-expression-escaping/52312 "2020-12-24T16:32:35Z")

</div>

This code: macro mymacro(ast) println("ast outside = ", ast, "---\\n") :(function generated() println("ast inside = ", $ast, "---\\n") end ) end @mymacro function func() return 123 end genera…

---

## [Get function as expr](https://discourse.julialang.org/t/get-function-as-expr/51886)

<div class="topic-metadata">

**Author:** [@Volker\_Weissmann](https://discourse.julialang.org/u/Volker_Weissmann)\
**Replies:** 5\
**Last updated:** [December 15, 2020, 6:22pm UTC](https://discourse.julialang.org/t/get-function-as-expr/51886 "2020-12-15T18:22:07Z")

</div>

My question is the same as Is there a way to get the expression object for a function? , but the old answer does not work in new julia versions. Lets say you defined method: function func() return 17 end I know th…

---

## [Scope of expressions returned from @generated functions](https://discourse.julialang.org/t/scope-of-expressions-returned-from-generated-functions/51241)

<div class="topic-metadata">

**Author:** [@dafred](https://discourse.julialang.org/u/dafred)\
**Replies:** 0\
**Last updated:** [December 4, 2020, 9:42am UTC](https://discourse.julialang.org/t/scope-of-expressions-returned-from-generated-functions/51241 "2020-12-04T09:42:17Z")

</div>

So I want to create models that have a similar calculation graph but differ in small aspects. Each model is represented as a struct with matching dispatches for function calc. To demonstrate what I have so far, I creat…

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

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