# Heavy macro use or not?

**URL:** <https://discourse.julialang.org/t/heavy-macro-use-or-not/115097>\
**Category:** Community\
**Created:** [June 3, 2024, 6:07am UTC](https://discourse.julialang.org/t/heavy-macro-use-or-not/115097 "2024-06-03T06:07:30Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [June 4, 2024, 4:08pm UTC](https://discourse.julialang.org/t/heavy-macro-use-or-not/115097/21 "2024-06-04T16:08:30Z")

</div>

Makes me wonder if a Terse.jl package would be worthwhile; exporting things like a little, boilerplate-reducing `@singleton` macro:

```julia
function f(@singleton(MY_CONST::MyType), x)
  println("Can I even do this in function arg scope?")
end
# expands the @singleton arg to `::MyType` and evals the below in module scope:
struct MyType end
const MY_CONST = MyType()

```

I kind of miss these kind of conciseness-affordance macros from lisps.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [June 4, 2024, 4:19pm UTC](https://discourse.julialang.org/t/heavy-macro-use-or-not/115097/22 "2024-06-04T16:19:01Z")

</div>

As an educator I am against macros because they are a fundamentally more advanced concept than typical programming that most academics would be familiar with. As a developer, I realize that macros are a powerful feature of the language that can be beneficial when used correctly. But they can be brutally hard to code and debug ☹ I still dont know how to write macros after 8 years of using the language.

However, I always, absolutely, without any shadow of a doubt, am 100% against using macros when a function would do exactly the same thing with the _same level of code complexity_.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [June 4, 2024, 5:00pm UTC](https://discourse.julialang.org/t/heavy-macro-use-or-not/115097/23 "2024-06-04T17:00:21Z")

</div>

The `@lift` macro in Makie and the DataFramesMeta macros do the same thing, they let you write an expression that resolves to a function, where otherwise you’d have to write each variable at least three times. Once as an input to the function, once as an argument, and once in the function body.

```julia
@lift $observable_a + $observable_b + 10
# is equivalent to
lift(observable_a, observable_b) do obs_a, obs_b
    obs_a + obs_b + 10
end

```

Vanilla DataFrames can be quite verbose due to this

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [June 4, 2024, 5:40pm UTC](https://discourse.julialang.org/t/heavy-macro-use-or-not/115097/24 "2024-06-04T17:40:24Z")

</div>

When I was working on [DuckDispatch.jl](https://github.com/mrufsvold/DuckDispatch.jl), I realized that the user would need to define multiple copies of each of the required methods for a new DuckType. One for fallback, and one that unwraps the DuckType wrapper can calls the original function. It also has a reasonably complex syntax for declaring the supertype of a new DuckType and that supertype needs to match the methods you made exactly. Then, there is a whole other set of functions that you need to define if you want to dispatch on a DuckType.

A declarative DSL driven by macros allows me to make sure all of that is synced up and throw informative errors if the user messes something up.

I plan to document exactly what the macros are producing and make all of it public, but I would see it as highly inadvisable to circumvent the macro API.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [June 5, 2024, 8:51am UTC](https://discourse.julialang.org/t/heavy-macro-use-or-not/115097/25 "2024-06-05T08:51:49Z")

</div>

`Base` already exports macros heavily, e.g. with `@inbounds`, `@enum` , `@view`, `@.` …

There are packages which wouldn’t make much sense without a macro interface, e.g. Match.jl, MLStyle.jl, Accessors.jl, ArgParse.jl, JuMP.jl.

I’m a bit bothered by the fact that [macros don’t compose well](https://github.com/JuliaLang/julia/issues/37691) and there’s no plan to fix this issue.

[Previous page](https://discourse.julialang.org/t/heavy-macro-use-or-not/115097.md?page=1)
