# \#metaprogramming

**URL:** https://discourse.julialang.org/tag/metaprogramming/68.md?page=7

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

**Page:** 8

---

## [Help understanding use of macros over functions that return expressions](https://discourse.julialang.org/t/help-understanding-use-of-macros-over-functions-that-return-expressions/51217)

<div class="topic-metadata">

**Author:** [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Replies:** 1\
**Last updated:** [December 3, 2020, 6:51pm UTC](https://discourse.julialang.org/t/help-understanding-use-of-macros-over-functions-that-return-expressions/51217 "2020-12-03T18:51:50Z")

</div>

The docs are clear about how macros execute before the enclosing code is run, but I’m still having trouble imagining where this is necessary, when we already have functions that return expressions. It’s easy to come up…

---

## [Metaprogramming and variable scope (again)](https://discourse.julialang.org/t/metaprogramming-and-variable-scope-again/49248)

<div class="topic-metadata">

**Author:** [@benkj](https://discourse.julialang.org/u/benkj)\
**Replies:** 3\
**Last updated:** [October 29, 2020, 5:27pm UTC](https://discourse.julialang.org/t/metaprogramming-and-variable-scope-again/49248 "2020-10-29T17:27:14Z")

</div>

Hi there, I’m having some troubles understanding variable scoping in macros. Here is the simplest code to show my problems macro foo(f) quote x = 2 $f end end function bar(y) @foo sum(x\*i f…

---

## [Dynamically Create a Function - Initial Idea with eval failed due to World Age Issue](https://discourse.julialang.org/t/dynamically-create-a-function-initial-idea-with-eval-failed-due-to-world-age-issue/49139)

<div class="topic-metadata">

**Author:** [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Replies:** 19\
**Last updated:** [October 28, 2020, 2:51pm UTC](https://discourse.julialang.org/t/dynamically-create-a-function-initial-idea-with-eval-failed-due-to-world-age-issue/49139 "2020-10-28T14:51:42Z")

</div>

Hey all, I’m struggling to implement a certain problem. My current situation is the following: Some parts of my project consist of using Tullio functions. Since my arrays can be of arbitrary dimensions and I want to ap…

---

## [Symbol to variable in a macro](https://discourse.julialang.org/t/symbol-to-variable-in-a-macro/48924)

<div class="topic-metadata">

**Author:** [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Replies:** 6\
**Last updated:** [October 24, 2020, 1:49pm UTC](https://discourse.julialang.org/t/symbol-to-variable-in-a-macro/48924 "2020-10-24T13:49:23Z")

</div>

An easy question. I have asked it some time back and got a good answer, but cannot find back to it, not on Discourse, not in the manual, and not in my code… I want to write a macro to generate code like getfield(vari,…

---

## [Check head of an expression](https://discourse.julialang.org/t/check-head-of-an-expression/48852)

<div class="topic-metadata">

**Author:** [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Replies:** 2\
**Last updated:** [October 23, 2020, 12:39pm UTC](https://discourse.julialang.org/t/check-head-of-an-expression/48852 "2020-10-23T12:39:08Z")

</div>

I Often have an expression t = :(y = f(:x)) I want to examine the head of the RHS, I can do this by looking at rhs = t.args\[2\] rhs.head ... Unfortunately, if you have t = :(y = :x)) then rhs is a QuoteNode and I ge…

---

## [Substituting Expression for Symbol in Nested Expression](https://discourse.julialang.org/t/substituting-expression-for-symbol-in-nested-expression/36366)

<div class="topic-metadata">

**Author:** [@r-b0](https://discourse.julialang.org/u/r-b0)\
**Replies:** 5\
**Last updated:** [October 22, 2020, 12:55pm UTC](https://discourse.julialang.org/t/substituting-expression-for-symbol-in-nested-expression/36366 "2020-10-22T12:55:54Z")

</div>

Let’s suppose I have a situation which necessitates substituting an expression for a symbol in a nested expression. Let’s further suppose that I am unable to acomplish this by acting on the string before parsing using s…

---

## [Indentifying pathological cases in IR form](https://discourse.julialang.org/t/indentifying-pathological-cases-in-ir-form/48746)

<div class="topic-metadata">

**Author:** [@wulpuqu](https://discourse.julialang.org/u/wulpuqu)\
**Replies:** 0\
**Last updated:** [October 21, 2020, 1:33pm UTC](https://discourse.julialang.org/t/indentifying-pathological-cases-in-ir-form/48746 "2020-10-21T13:33:54Z")

</div>

Hello everyone, I am currently playing with lowered code and I would like to understand how to identify pathological cases. In the following example, a variable b gets conditionnaly assigned at step 1 of the loop and th…

---

## [Determine the scope of symbols in an Expr](https://discourse.julialang.org/t/determine-the-scope-of-symbols-in-an-expr/47993)

<div class="topic-metadata">

**Author:** [@Mason](https://discourse.julialang.org/u/Mason)\
**Replies:** 1\
**Last updated:** [October 8, 2020, 5:17pm UTC](https://discourse.julialang.org/t/determine-the-scope-of-symbols-in-an-expr/47993 "2020-10-08T17:17:48Z")

</div>

I asked about this on Zulip and cleaned it up and posted to Stack Overflow but I figure it might be helpful to cross pollinate here as well. I know I’ve wondered how to do this a few times, so probably other people have …

---

## [How best to write this macro?](https://discourse.julialang.org/t/how-best-to-write-this-macro/47817)

<div class="topic-metadata">

**Author:** [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Replies:** 8\
**Last updated:** [October 6, 2020, 12:27am UTC](https://discourse.julialang.org/t/how-best-to-write-this-macro/47817 "2020-10-06T00:27:48Z")

</div>

I’m learning about meta programming and as practice, I tried to define a macro that would batch define and initialize arrays of a certain type. I got some of the way, but it’s clunky. What would be a better way of writ…

---

## [How to pass a relative UnitRange to a function?](https://discourse.julialang.org/t/how-to-pass-a-relative-unitrange-to-a-function/47431)

<div class="topic-metadata">

**Author:** [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Replies:** 9\
**Last updated:** [September 29, 2020, 10:10pm UTC](https://discourse.julialang.org/t/how-to-pass-a-relative-unitrange-to-a-function/47431 "2020-09-29T22:10:46Z")

</div>

I have the following which tries to apply a relative unit range to a string whose length is unknown: function testnums(data::String,numrange::String) data\[numrange\] end foo = "asdfasdfasdf" bar = "end-3:end" testnu…

---

## [Help generating an Expression](https://discourse.julialang.org/t/help-generating-an-expression/47283)

<div class="topic-metadata">

**Author:** [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Replies:** 3\
**Last updated:** [September 25, 2020, 7:34pm UTC](https://discourse.julialang.org/t/help-generating-an-expression/47283 "2020-09-25T19:34:09Z")

</div>

I have some wide-formatted data that I need to group and summarize. A MWE is below: using DataFramesMeta df = DataFrame( id = repeat(1:5, inner=3), a\_1 = rand(15), a\_2 = rand(15), a\_3 = rand(15), b\_…

---

## [Evaluating a SyntaxTree.genfun within a function](https://discourse.julialang.org/t/evaluating-a-syntaxtree-genfun-within-a-function/46744)

<div class="topic-metadata">

**Author:** [@Jhall7529](https://discourse.julialang.org/u/Jhall7529)\
**Replies:** 6\
**Last updated:** [September 23, 2020, 9:30pm UTC](https://discourse.julialang.org/t/evaluating-a-syntaxtree-genfun-within-a-function/46744 "2020-09-23T21:30:08Z")

</div>

Im trying to evaluate a string as a function within a function. I have made good progress: using SyntaxTree function Evaluate(some\_function::String ; variables = \[:x\]) g = genfun(Meta.parse(some\_function), variables…

---

## [Call a constructor by interpolation](https://discourse.julialang.org/t/call-a-constructor-by-interpolation/46732)

<div class="topic-metadata">

**Author:** [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Replies:** 2\
**Last updated:** [September 16, 2020, 10:51pm UTC](https://discourse.julialang.org/t/call-a-constructor-by-interpolation/46732 "2020-09-16T22:51:46Z")

</div>

I need to programmatically initialize an empty DataFrame using a vector of names and a vector of types: Here is an example of doing this explicitly: DataFrame(mystrings=String\[\],myints=Int64\[\]) 0×2 DataFrame MWE for d…

---

## [How to use a variable value inside @tullio](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172)

<div class="topic-metadata">

**Author:** [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Replies:** 12\
**Last updated:** [September 7, 2020, 8:12pm UTC](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172 "2020-09-07T20:12:54Z")

</div>

What’s the proper syntax to get this to run? using Tullio function ugh(tf::Bool) X = randn(10000,100) y = zeros(100) @tullio threads=tf y\[j\] = X\[i,j\] end ugh(true)

---

## [Julia learning macros / metaprogramming](https://discourse.julialang.org/t/julia-learning-macros-metaprogramming/45753)

<div class="topic-metadata">

**Author:** [@Marcell\_Havlik](https://discourse.julialang.org/u/Marcell_Havlik)\
**Replies:** 13\
**Last updated:** [September 1, 2020, 8:48am UTC](https://discourse.julialang.org/t/julia-learning-macros-metaprogramming/45753 "2020-09-01T08:48:17Z")

</div>

Hey Julianers, I am trieing to understand the julia macros, but I just can’t understand when do I have to use esc, quote, $, eval or so… Can someone give me the easiest tutorial I can understand? How do you know when t…

---

## [Emit an AST as Julia code?](https://discourse.julialang.org/t/emit-an-ast-as-julia-code/45368)

<div class="topic-metadata">

**Author:** [@stevenbarrett](https://discourse.julialang.org/u/stevenbarrett)\
**Replies:** 3\
**Last updated:** [August 23, 2020, 4:51am UTC](https://discourse.julialang.org/t/emit-an-ast-as-julia-code/45368 "2020-08-23T04:51:12Z")

</div>

Hi there! Does anyone know if there a function or library to emit an AST as Julia code (i.e. convert an AST back into normal human-readable Julia)?

---

## [Classification of numbers using metaprogramming](https://discourse.julialang.org/t/classification-of-numbers-using-metaprogramming/43775)

<div class="topic-metadata">

**Author:** [@j\_tanu](https://discourse.julialang.org/u/j_tanu)\
**Replies:** 4\
**Last updated:** [July 27, 2020, 5:36pm UTC](https://discourse.julialang.org/t/classification-of-numbers-using-metaprogramming/43775 "2020-07-27T17:36:55Z")

</div>

Hi, I am trying to classify a large set of floating-point numbers in various range and count them. I want to divide them into a large no of classes. Practically it is impossible to write every clause using an if-else ki…

---

## [Understanding the limitations of generated functions](https://discourse.julialang.org/t/understanding-the-limitations-of-generated-functions/43660)

<div class="topic-metadata">

**Author:** [@tisztamo](https://discourse.julialang.org/u/tisztamo)\
**Replies:** 17\
**Last updated:** [July 27, 2020, 12:26pm UTC](https://discourse.julialang.org/t/understanding-the-limitations-of-generated-functions/43660 "2020-07-27T12:26:23Z")

</div>

The docs says: Generated functions must not mutate or observe any non-constant global state (including, for example, IO, locks, non-local dictionaries, or using hasmethod ). This means they can only read global c…

---

## [When should a function accept a symbol as an argument?](https://discourse.julialang.org/t/when-should-a-function-accept-a-symbol-as-an-argument/43510)

<div class="topic-metadata">

**Author:** [@clarkfitzg](https://discourse.julialang.org/u/clarkfitzg)\
**Replies:** 40\
**Last updated:** [July 26, 2020, 4:17pm UTC](https://discourse.julialang.org/t/when-should-a-function-accept-a-symbol-as-an-argument/43510 "2020-07-26T16:17:01Z")

</div>

I stumbled across two situations where functions accept symbols as arguments, and wonder why they are designed that way. What advantages are there to using symbols? When should I write functions that accept symbols inste…

---

## [How to print function name and source file/line number](https://discourse.julialang.org/t/how-to-print-function-name-and-source-file-line-number/43486)

<div class="topic-metadata">

**Author:** [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Replies:** 3\
**Last updated:** [July 22, 2020, 1:21pm UTC](https://discourse.julialang.org/t/how-to-print-function-name-and-source-file-line-number/43486 "2020-07-22T13:21:21Z")

</div>

Hello, is it possible to print the current function name and the REPL/Juno/VSCode clickable file name/function name, e.g. function foo() @codeLocation ... end Resulting in: Running function foo on /path/to/myf…

---

## [Macro that defines function](https://discourse.julialang.org/t/macro-that-defines-function/42969)

<div class="topic-metadata">

**Author:** [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Replies:** 2\
**Last updated:** [July 13, 2020, 5:48am UTC](https://discourse.julialang.org/t/macro-that-defines-function/42969 "2020-07-13T05:48:53Z")

</div>

This doesn’t work. What’s the reason why I can’t define a function as expanded from a macro? Or did I do something wrong? module C macro makefunc(ex) return quote wat() = $(esc(ex)) e…

---

## [Improving macro programming](https://discourse.julialang.org/t/improving-macro-programming/42755)

<div class="topic-metadata">

**Author:** [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Replies:** 10\
**Last updated:** [July 9, 2020, 10:10am UTC](https://discourse.julialang.org/t/improving-macro-programming/42755 "2020-07-09T10:10:26Z")

</div>

I’m learning macro programming and as an experiment I programmed up a macro version of map!. It works, but seems clunky with all the escapes. What would be a more elegant way of programming the macro? I can define lo…

---

## [Adding methods with metaprogramming](https://discourse.julialang.org/t/adding-methods-with-metaprogramming/42518)

<div class="topic-metadata">

**Author:** [@sw72](https://discourse.julialang.org/u/sw72)\
**Replies:** 1\
**Last updated:** [July 4, 2020, 11:58am UTC](https://discourse.julialang.org/t/adding-methods-with-metaprogramming/42518 "2020-07-04T11:58:12Z")

</div>

I have a couple of types defined like this: struct Foo foo end struct Bar foo::Foo bar baz end Then I have a bunch of functions defined for Foo, with additional methods for Bar that just pass through t…

---

## [Metaprogamming](https://discourse.julialang.org/t/metaprogamming/42286)

<div class="topic-metadata">

**Author:** [@Azzaare](https://discourse.julialang.org/u/Azzaare)\
**Replies:** 5\
**Last updated:** [June 30, 2020, 6:38am UTC](https://discourse.julialang.org/t/metaprogamming/42286 "2020-06-30T06:38:50Z")

</div>

Hi there, I am writing a module with dozens of MyType like structure (as below), that all belong to an Abstract super type. I would like to be able to write a meta constructor (roughly sketched below). Is it possible? s…

---

## [Custom IO performance - loading variables from byte array](https://discourse.julialang.org/t/custom-io-performance-loading-variables-from-byte-array/41814)

<div class="topic-metadata">

**Author:** [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Replies:** 2\
**Last updated:** [June 23, 2020, 9:39am UTC](https://discourse.julialang.org/t/custom-io-performance-loading-variables-from-byte-array/41814 "2020-06-23T09:39:46Z")

</div>

I want to load several variables of different bitstypes from a packed byte vector. For example, Int16, Int32, Int16 from 8-byte chunk of memory. I’ve tried several approaches to find a most efficient way: Using IOBuf…

---

## [Use @eval within a function](https://discourse.julialang.org/t/use-eval-within-a-function/40451)

<div class="topic-metadata">

**Author:** [@Lucas\_Liu](https://discourse.julialang.org/u/Lucas_Liu)\
**Replies:** 4\
**Last updated:** [May 30, 2020, 2:41am UTC](https://discourse.julialang.org/t/use-eval-within-a-function/40451 "2020-05-30T02:41:58Z")

</div>

I am new to metaprograming in Julia. The case is I have many vectors to initialize in a constructor. I came up with this code function MyStruct(n::Int) vecs = \[:x, :y, :z, :p\] for v in vecs @eval $v = Ve…

---

## [Generating function for multi-dimensional array indexing](https://discourse.julialang.org/t/generating-function-for-multi-dimensional-array-indexing/40342)

<div class="topic-metadata">

**Author:** [@Alex\_Ellison](https://discourse.julialang.org/u/Alex_Ellison)\
**Replies:** 2\
**Last updated:** [May 28, 2020, 2:42pm UTC](https://discourse.julialang.org/t/generating-function-for-multi-dimensional-array-indexing/40342 "2020-05-28T14:42:00Z")

</div>

Hey all, I’m trying to generate some functions, and while they work, I don’t fully understand the behavior coming out. Also, there may be some function in Base which does this, I just can’t find it. Illumination of eith…

---

## [Printing of an Expr seems wrong](https://discourse.julialang.org/t/printing-of-an-expr-seems-wrong/40051)

<div class="topic-metadata">

**Author:** [@Stephan\_Meyn](https://discourse.julialang.org/u/Stephan_Meyn)\
**Replies:** 2\
**Last updated:** [May 24, 2020, 5:18am UTC](https://discourse.julialang.org/t/printing-of-an-expr-seems-wrong/40051 "2020-05-24T05:18:16Z")

</div>

I have an EXPR. dump() shows up as follows: dump(x) Expr head: Symbol :: args: Array{Any}((2,)) 1: Symbol afield 2: Symbol Union{Nothing, Int32} put print shows up as: print(x) afield::var"Union{Nothing, Int…

---

## [What should be an argument of this function?](https://discourse.julialang.org/t/what-should-be-an-argument-of-this-function/39660)

<div class="topic-metadata">

**Author:** [@wiktorkujawa](https://discourse.julialang.org/u/wiktorkujawa)\
**Replies:** 3\
**Last updated:** [May 18, 2020, 9:13am UTC](https://discourse.julialang.org/t/what-should-be-an-argument-of-this-function/39660 "2020-05-18T09:13:15Z")

</div>

Here’e the code error. julia\> name(15) ERROR: MethodError: no method matching name(::Int64) Closest candidates are: name(::DataType) I tried also this: julia\> name(typeof(Int)) ERROR: MethodError: Cannot \`convert\` a…

---

## [Running macros with expression as an argument](https://discourse.julialang.org/t/running-macros-with-expression-as-an-argument/39584)

<div class="topic-metadata">

**Author:** [@wiktorkujawa](https://discourse.julialang.org/u/wiktorkujawa)\
**Replies:** 4\
**Last updated:** [May 16, 2020, 10:45pm UTC](https://discourse.julialang.org/t/running-macros-with-expression-as-an-argument/39584 "2020-05-16T22:45:36Z")

</div>

I’ve got errors on “local first\_parameter”. If I try to mess something without any sense, sometimes I get error at “local arg1” line. macro howToRunThis(fun::Expr) local first\_parameter = fun.args\[1\] local name = fi…

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

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