# Why \`@nospecialize(ex)\` in macros?

**URL:** <https://discourse.julialang.org/t/why-nospecialize-ex-in-macros/91589>\
**Category:** Performance\
**Tags:** macros\
**Created:** [December 13, 2022, 9:24am UTC](https://discourse.julialang.org/t/why-nospecialize-ex-in-macros/91589 "2022-12-13T09:24:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Post date:** [December 13, 2022, 9:24am UTC](https://discourse.julialang.org/t/why-nospecialize-ex-in-macros/91589/1 "2022-12-13T09:24:41Z")

</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](https://discourse.julialang.org/t/typed-globals-macro-to-define-multiple-without-manual-type-annotations/90818/2)

I’d say the only types the argument could be are `Expr` and `LineNumberNode`, so there’s not a whole lot to optimize by avoiding type specialization.  
(Maybe `ex` could also be some ‘atom’/literal type I guess)

---

<div class="post-metadata">

**Author:** ![aviatesk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aviatesk/32/7610_2.png) [@aviatesk](https://discourse.julialang.org/u/aviatesk)\
**Post date:** [December 14, 2022, 8:26am UTC](https://discourse.julialang.org/t/why-nospecialize-ex-in-macros/91589/2 "2022-12-14T08:26:29Z")

</div>

The short answer is because:

- the type of `ex` isn’t known when `@typed ex` is called
- it’s not very important to specialize `rewrite_assignment` on `ex`

And so for this kind of case `@nospecialize` can avoid runtime dispatch on `rewrite_assignment` and will slightly improve the macro expansion of `@typed` (although it may not be very important given that the computation happens at lowering time).

> [@tfiers](#):
>
> I’d say the only types the argument could be are `Expr` and `LineNumberNode`, so there’s not a whole lot to optimize by avoiding type specialization.

Well, `rewrite_assignment` is defined in a way that it can take arbitrary AST elements other than `Expr` and `LineNumberNode` e.g. `Int`, `Symbol`. I agree with that it may not be very important to optimize the computation that only happens at lowering time, but it’s certainly better to have `@nospecialze` there.

---

<div class="post-metadata">

**Author:** ![aviatesk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aviatesk/32/7610_2.png) [@aviatesk](https://discourse.julialang.org/u/aviatesk)\
**Post date:** [December 14, 2022, 8:28am UTC](https://discourse.julialang.org/t/why-nospecialize-ex-in-macros/91589/3 "2022-12-14T08:28:04Z")

</div>

If you want to have a more understanding on what `@nospecialize` really does, the description of [this PR](https://github.com/JuliaLang/julia/pull/41931) may give some idea.
