# \#macros

**URL:** https://discourse.julialang.org/tag/macros/67.md?match_all_tags=true&page=1&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:** 2

---

## [SQL and Macros](https://discourse.julialang.org/t/sql-and-macros/124673)

<div class="topic-metadata">

**Author:** [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Replies:** 1\
**Last updated:** [January 11, 2025, 3:59pm UTC](https://discourse.julialang.org/t/sql-and-macros/124673 "2025-01-11T15:59:21Z")

</div>

I have the following script that runs fine: using DuckDB using DBInterface using DataFrames # Global connection variable for reuse const duckdb\_connection = Ref{Union{DuckDB.DB, Nothing}}(nothing) # Function to manage…

---

## [Generate a function via macro and pass to @cfunction. Possible?](https://discourse.julialang.org/t/generate-a-function-via-macro-and-pass-to-cfunction-possible/123988)

<div class="topic-metadata">

**Author:** [@tqml](https://discourse.julialang.org/u/tqml)\
**Replies:** 7\
**Last updated:** [December 20, 2024, 11:59pm UTC](https://discourse.julialang.org/t/generate-a-function-via-macro-and-pass-to-cfunction-possible/123988 "2024-12-20T23:59:09Z")

</div>

I’m trying to write a macro that takes a function, creates a wrapper for it, passes the wrapper to @cfunction and finally passes the function pointer to an external C library. To further clarify the use-case here: I wan…

---

## [Adding a parameter to macro call](https://discourse.julialang.org/t/adding-a-parameter-to-macro-call/121489)

<div class="topic-metadata">

**Author:** [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Replies:** 1\
**Last updated:** [October 20, 2024, 6:17am UTC](https://discourse.julialang.org/t/adding-a-parameter-to-macro-call/121489 "2024-10-20T06:17:17Z")

</div>

In my package ShareAdd.jl a macro is defined in the form(s) @usingany Pkg1 @usingany Pkg1, Pkg2, Pkg3 Now, I’d like to add an optional flag to first update the corresponding packages before import. What is the (best) w…

---

## [How use eval to define macros that involve function definitions](https://discourse.julialang.org/t/how-use-eval-to-define-macros-that-involve-function-definitions/93773)

<div class="topic-metadata">

**Author:** [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Replies:** 3\
**Last updated:** [October 19, 2024, 6:12pm UTC](https://discourse.julialang.org/t/how-use-eval-to-define-macros-that-involve-function-definitions/93773 "2024-10-19T18:12:58Z")

</div>

I want to define a bunch of macros that define function methods in a private module. module MyModule function f() end function g() end end using Main.MyModule struct T i end macro f(T, expr) esc(:(MyModule.…

---

## [Macro to reorder conditional nested loop](https://discourse.julialang.org/t/macro-to-reorder-conditional-nested-loop/93588)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 4\
**Last updated:** [October 18, 2024, 5:31pm UTC](https://discourse.julialang.org/t/macro-to-reorder-conditional-nested-loop/93588 "2024-10-18T17:31:33Z")

</div>

I have a loop of the form for j=1:n for k = max(1,j-d\_u):min(j-u-1,m) f(j, k) end end and it turns out that computing the limits on k in each iteration is adding a lot of overhead. Turns ou…

---

## [Unexpected Results for Code Coverage with Julia Tests](https://discourse.julialang.org/t/unexpected-results-for-code-coverage-with-julia-tests/119195)

<div class="topic-metadata">

**Author:** [@janneshb](https://discourse.julialang.org/u/janneshb)\
**Replies:** 2\
**Last updated:** [October 18, 2024, 12:50pm UTC](https://discourse.julialang.org/t/unexpected-results-for-code-coverage-with-julia-tests/119195 "2024-10-18T12:50:23Z")

</div>

Hi, I’ve written a bunch of unit tests for my Julia package that are run by a GitHub action and uploaded to codecov.io. However, lines marked as “uncovered” seem to be wrong at a few spots. Especially, when macros are…

---

## [IDE: step-through debugging for macro-generated function?](https://discourse.julialang.org/t/ide-step-through-debugging-for-macro-generated-function/63899)

<div class="topic-metadata">

**Author:** [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Replies:** 0\
**Last updated:** [July 1, 2021, 2:59pm UTC](https://discourse.julialang.org/t/ide-step-through-debugging-for-macro-generated-function/63899 "2021-07-01T14:59:52Z")

</div>

I recently tried using VSCode debugger to “step into” a function from an external package, but VSCode pointed me to a strange code location. Eventually I figured out that the function was not written out, but was generat…

---

## [Unexpected result from setprecision](https://discourse.julialang.org/t/unexpected-result-from-setprecision/89336)

<div class="topic-metadata">

**Author:** [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Replies:** 3\
**Last updated:** [October 18, 2024, 1:55am UTC](https://discourse.julialang.org/t/unexpected-result-from-setprecision/89336 "2024-10-18T01:55:09Z")

</div>

I expected the following to return 512, rather than 256. Could someone please explain why this is happening? julia\> setprecision(512) do thisbig = big"1.0" @show precision(thisbig) end prec…

---

## [FrankenTuple Fun](https://discourse.julialang.org/t/frankentuple-fun/91507)

<div class="topic-metadata">

**Author:** [@uniment](https://discourse.julialang.org/u/uniment)\
**Replies:** 0\
**Last updated:** [December 10, 2022, 11:18am UTC](https://discourse.julialang.org/t/frankentuple-fun/91507 "2022-12-10T11:18:32Z")

</div>

Yes, but (a; b) is horrible. That should be syntax for FrankenTuples . and and Turns out that (a,; b) parses, and has for awhile (according to this time machine): An experiment with FrankenTuples: julia\> us…

---

## [Is there a built in macro like @show that returns a string?](https://discourse.julialang.org/t/is-there-a-built-in-macro-like-show-that-returns-a-string/88234)

<div class="topic-metadata">

**Author:** [@taotree](https://discourse.julialang.org/u/taotree)\
**Replies:** 9\
**Last updated:** [October 13, 2024, 11:41pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-macro-like-show-that-returns-a-string/88234 "2024-10-13T23:41:40Z")

</div>

I want the macro @show but to return a string instead of printing. Does something like that already exist?

---

## [String macro with trailing argument](https://discourse.julialang.org/t/string-macro-with-trailing-argument/121162)

<div class="topic-metadata">

**Author:** [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Replies:** 1\
**Last updated:** [October 10, 2024, 5:39pm UTC](https://discourse.julialang.org/t/string-macro-with-trailing-argument/121162 "2024-10-10T17:39:25Z")

</div>

I am trying to get the string literal macro example with a trailing argument from the manual to work (end of section linked), with the MWE struct Foo str::String flag::Bool end macro foo\_str(str, flag) \_fla…

---

## [Macro usage: create dispatch for vector length](https://discourse.julialang.org/t/macro-usage-create-dispatch-for-vector-length/118142)

<div class="topic-metadata">

**Author:** [@heronrick](https://discourse.julialang.org/u/heronrick)\
**Replies:** 8\
**Last updated:** [September 26, 2024, 3:44pm UTC](https://discourse.julialang.org/t/macro-usage-create-dispatch-for-vector-length/118142 "2024-09-26T15:44:56Z")

</div>

Hello, I have an Idea I would like to implement for a module I am working on. I will try to explain, but I am not sure if such implementation would make much sense, probably there are better solutions. I have a macro, …

---

## [Create dynamic functions with Macro](https://discourse.julialang.org/t/create-dynamic-functions-with-macro/119824)

<div class="topic-metadata">

**Author:** [@Ronneesley](https://discourse.julialang.org/u/Ronneesley)\
**Replies:** 15\
**Last updated:** [September 26, 2024, 12:33am UTC](https://discourse.julialang.org/t/create-dynamic-functions-with-macro/119824 "2024-09-26T00:33:22Z")

</div>

Hi, I’m trying create “dynamic” functions with macro, but it isn’t working. Let-me explain: I’ve this function: function f(::Type{T}, message) where T println("Type: ", T) println("Message: ", message) end Wh…

---

## [Macro expansion and evaluation when the module with the macro definition is not in scope](https://discourse.julialang.org/t/macro-expansion-and-evaluation-when-the-module-with-the-macro-definition-is-not-in-scope/119170)

<div class="topic-metadata">

**Author:** [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Replies:** 2\
**Last updated:** [September 7, 2024, 4:16pm UTC](https://discourse.julialang.org/t/macro-expansion-and-evaluation-when-the-module-with-the-macro-definition-is-not-in-scope/119170 "2024-09-07T16:16:36Z")

</div>

Let’s say I’m developing a macro @mymacro in a package called CoolStuff. Suppose that this @mymacro 42 expands to this: CoolStuff.foo(42) Now suppose that the user loads @mymacro by doing using CoolStuff: @mymacro in…

---

## [About the interpretation "@" by Macro](https://discourse.julialang.org/t/about-the-interpretation-by-macro/119155)

<div class="topic-metadata">

**Author:** [@lionisxn](https://discourse.julialang.org/u/lionisxn)\
**Replies:** 2\
**Last updated:** [September 7, 2024, 4:21am UTC](https://discourse.julialang.org/t/about-the-interpretation-by-macro/119155 "2024-09-07T04:21:31Z")

</div>

I have a macro defined as follows: macro A(x…) x …(other codes) end When I called “@A code@W”, the input x was interpreted by @A as two elements of \[:code, :W\], how can I prevent “@” from interpretation?

---

## [What are the dangers of eval in macros, to get the values of an enum symbol](https://discourse.julialang.org/t/what-are-the-dangers-of-eval-in-macros-to-get-the-values-of-an-enum-symbol/119078)

<div class="topic-metadata">

**Author:** [@NatMath](https://discourse.julialang.org/u/NatMath)\
**Replies:** 3\
**Last updated:** [September 6, 2024, 6:22pm UTC](https://discourse.julialang.org/t/what-are-the-dangers-of-eval-in-macros-to-get-the-values-of-an-enum-symbol/119078 "2024-09-06T18:22:28Z")

</div>

I’m working on an implementation of the PBRT 4 raytracer in Julia. To avoid having containers of abstract types for primitives, materials, etc, some of the base object types are defined using a tag, instead of subclasses…

---

## [Julia code and struct generation](https://discourse.julialang.org/t/julia-code-and-struct-generation/119044)

<div class="topic-metadata">

**Author:** [@devel-chm](https://discourse.julialang.org/u/devel-chm)\
**Replies:** 12\
**Last updated:** [September 6, 2024, 12:32pm UTC](https://discourse.julialang.org/t/julia-code-and-struct-generation/119044 "2024-09-06T12:32:56Z")

</div>

I would like to generate a bunch of types for each possible file data format. There is a header with a nibble, X, that determines which fields are present. struct A\_X \<: B # X == 0x0 .. 0xF vall:…

---

## [Naming macro and function the same](https://discourse.julialang.org/t/naming-macro-and-function-the-same/117959)

<div class="topic-metadata">

**Author:** [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Replies:** 7\
**Last updated:** [August 9, 2024, 5:26am UTC](https://discourse.julialang.org/t/naming-macro-and-function-the-same/117959 "2024-08-09T05:26:13Z")

</div>

In the Source code of Reexport, they define a macro Reexport, inmediately define a behaviour (I assume macros can also have multiple dispatch…?) and then they define a function with the same name: macro reexport(ex::Exp…

---

## [Creating a conditional log](https://discourse.julialang.org/t/creating-a-conditional-log/117932)

<div class="topic-metadata">

**Author:** [@korch](https://discourse.julialang.org/u/korch)\
**Replies:** 9\
**Last updated:** [August 8, 2024, 12:47pm UTC](https://discourse.julialang.org/t/creating-a-conditional-log/117932 "2024-08-08T12:47:40Z")

</div>

Hi there! TLDR: I want to make a logger for my code that outputs the unevaluated expressions and evaluated expression sometimes - like on one time step. It seems the only way to do this is with a macro, but I can’t figu…

---

## [Macro to turn on/off parallelisation](https://discourse.julialang.org/t/macro-to-turn-on-off-parallelisation/117808)

<div class="topic-metadata">

**Author:** [@jibe](https://discourse.julialang.org/u/jibe)\
**Replies:** 15\
**Last updated:** [August 5, 2024, 4:50am UTC](https://discourse.julialang.org/t/macro-to-turn-on-off-parallelisation/117808 "2024-08-05T04:50:32Z")

</div>

I want to define a macro, @parallel, to turn on/off parallelisation in for loops, depending on a command-line argument. Here, parallelisation is embarassing and uses Threads.@threads. Why would I want to do this? Becaus…

---

## [Macro with variable argument list combined with closure](https://discourse.julialang.org/t/macro-with-variable-argument-list-combined-with-closure/117623)

<div class="topic-metadata">

**Author:** [@nietzsche](https://discourse.julialang.org/u/nietzsche)\
**Replies:** 2\
**Last updated:** [July 30, 2024, 11:19am UTC](https://discourse.julialang.org/t/macro-with-variable-argument-list-combined-with-closure/117623 "2024-07-30T11:19:06Z")

</div>

Hi, i have a question about variable arguments in macros together with the scope of them. Function count\_test() should be a closure over the variable count and the lambda should be generated with a macro. It should b…

---

## [Metaprogramming - Variable scope in macros combined with closures](https://discourse.julialang.org/t/metaprogramming-variable-scope-in-macros-combined-with-closures/117594)

<div class="topic-metadata">

**Author:** [@nietzsche](https://discourse.julialang.org/u/nietzsche)\
**Replies:** 4\
**Last updated:** [July 29, 2024, 2:54pm UTC](https://discourse.julialang.org/t/metaprogramming-variable-scope-in-macros-combined-with-closures/117594 "2024-07-29T14:54:48Z")

</div>

Hi, this is my first post. I have a question about macros and closures. The example is from the Book “LetOverLambda” Chapter 5.7. The author called it DLambda. The following source is a easy counter closure function c…

---

## [Wrapping Unitful macros](https://discourse.julialang.org/t/wrapping-unitful-macros/95171)

<div class="topic-metadata">

**Author:** [@jar1](https://discourse.julialang.org/u/jar1)\
**Replies:** 2\
**Last updated:** [March 18, 2023, 9:44pm UTC](https://discourse.julialang.org/t/wrapping-unitful-macros/95171 "2023-03-18T21:44:54Z")

</div>

Inspired by a commenter on Unitful.jl I’m trying to do unitful calculations with bookshelves: how many books do I have in 10 book cases? (20book / shelf) \* (8shelf / case) \* (10case) == 1600book I would like to replace…

---

## [\[ANN\] StrucRev - revise type definitions as you go](https://discourse.julialang.org/t/ann-strucrev-revise-type-definitions-as-you-go/109959)

<div class="topic-metadata">

**Author:** [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Replies:** 17\
**Last updated:** [July 28, 2024, 3:06pm UTC](https://discourse.julialang.org/t/ann-strucrev-revise-type-definitions-as-you-go/109959 "2024-07-28T15:06:50Z")

</div>

The package is intended to enable redefining structs within Revise.jl based workflow. It exports the macro @strev which (temporarily) wraps your struct like this @strev struct S0 x end becomes module StrucRev\_S0 …

---

## [Code Generation with macro](https://discourse.julialang.org/t/code-generation-with-macro/117080)

<div class="topic-metadata">

**Author:** [@Andy\_Zhang](https://discourse.julialang.org/u/Andy_Zhang)\
**Replies:** 6\
**Last updated:** [July 22, 2024, 12:58pm UTC](https://discourse.julialang.org/t/code-generation-with-macro/117080 "2024-07-22T12:58:38Z")

</div>

Background I am writing a package that provides some structures of matrices, and I want them to have different properties. module MyModuleA export TypeA, properties # function interfaces properties(::Type{\<:Ab…

---

## [Create a custom @ccall](https://discourse.julialang.org/t/create-a-custom-ccall/117243)

<div class="topic-metadata">

**Author:** [@chkat](https://discourse.julialang.org/u/chkat)\
**Replies:** 4\
**Last updated:** [July 22, 2024, 12:20pm UTC](https://discourse.julialang.org/t/create-a-custom-ccall/117243 "2024-07-22T12:20:54Z")

</div>

I am trying to create a macro that replaces the @ccall in my code. My idea is to create something like this: macro my\_ccall(arg) custom\_function\_before\_ccall() @ccall arg end I am having trouble with the typ…

---

## [Metaprogramming: substitute the name of the argument](https://discourse.julialang.org/t/metaprogramming-substitute-the-name-of-the-argument/116867)

<div class="topic-metadata">

**Author:** [@dmi3kno](https://discourse.julialang.org/u/dmi3kno)\
**Replies:** 20\
**Last updated:** [July 14, 2024, 8:08am UTC](https://discourse.julialang.org/t/metaprogramming-substitute-the-name-of-the-argument/116867 "2024-07-14T08:08:53Z")

</div>

I am writing a function factory that takes two functions and weighs them by some \_wt \\in (0,1). function fun\_mix(fun1::Function, fun2::Function; \_nm\_wt="\_wt") f = function (u::AbstractFloat, args...; \_wt=0.5) …

---

## [Testing error thrown by a macro](https://discourse.julialang.org/t/testing-error-thrown-by-a-macro/116983)

<div class="topic-metadata">

**Author:** [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Replies:** 2\
**Last updated:** [July 13, 2024, 2:14am UTC](https://discourse.julialang.org/t/testing-error-thrown-by-a-macro/116983 "2024-07-13T02:14:47Z")

</div>

Is there a standard way to test that a macro throws an error for certain inputs? Consider the following example: macro foo(x) x == :a || throw(ArgumentError("oops.")) 1 end If I use @test\_throws normally, it do…

---

## [\[Ann\] LightSumTypes.jl - Combine multiple types in a single one](https://discourse.julialang.org/t/ann-lightsumtypes-jl-combine-multiple-types-in-a-single-one/109204)

<div class="topic-metadata">

**Author:** [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Replies:** 5\
**Last updated:** [July 6, 2024, 1:01am UTC](https://discourse.julialang.org/t/ann-lightsumtypes-jl-combine-multiple-types-in-a-single-one/109204 "2024-07-06T01:01:47Z")

</div>

I’m happy to announce MixedStructTypes.jl which allows to compactify multiple mutable and immutable types in a single one with a similar syntax to the one of structs. They work identical to structs for many operations be…

---

## [How to save @macroexpand output to file on windows?](https://discourse.julialang.org/t/how-to-save-macroexpand-output-to-file-on-windows/116452)

<div class="topic-metadata">

**Author:** [@freedomjin](https://discourse.julialang.org/u/freedomjin)\
**Replies:** 1\
**Last updated:** [July 1, 2024, 1:55am UTC](https://discourse.julialang.org/t/how-to-save-macroexpand-output-to-file-on-windows/116452 "2024-07-01T01:55:04Z")

</div>

Hi, I’m studying someone else’s macro code, and would like to save the generated code to a file using @macroexpand and check out what is been done. But @macroexpand does not seem to return a string or any object, it only…

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

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