# Local macros

**URL:** <https://discourse.julialang.org/t/local-macros/125539>\
**Category:** General Usage\
**Tags:** macros, metaprogramming\
**Created:** [February 4, 2025, 3:46pm UTC](https://discourse.julialang.org/t/local-macros/125539 "2025-02-04T15:46:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Post date:** [February 4, 2025, 3:46pm UTC](https://discourse.julialang.org/t/local-macros/125539/1 "2025-02-04T15:46:15Z")

</div>

In CommonLisp one can define local macros using MACROLET. Julia doesn’t have local macros, but one can imagine that a given global macro could provide local macros for use wwithin its body using a code walker and rules that re-write `:macrocall` expressions.

Has anyone already implemented such a utility?

It seems straightforward enough, but I imagine there might be unforseen pitfalls.

What’s the problem I’m immediately trying to solve via local macros? I have a simple-minded expert system shell (Rete.jl) with an `@rule` macro. That macro implements an `install` method which will connect a new join node that implements the rule into a discrimination network. The body of a rule performs some conditionals and if all succeed then the rule will emit new facts to the network. Currently there are no diagnostics if a rule fails to make an expected conclusion if one of the conditionals fails because the join function just returns. I want there to be something (a local macro) that takes a predicate and logs when the predicate fails before the rule body exits.

This could be done with a closure but I want the log message to include the predicate expression and the source location.

I’m currently looking at both MacroTools and SymbolicUtils.

Any suggestions or insights?

Thanks.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 4, 2025, 4:05pm UTC](https://discourse.julialang.org/t/local-macros/125539/2 "2025-02-04T16:05:36Z")

</div>

> [@Mark\_Nahabedian](#):
>
> Julia doesn’t have local macros

Macros are scoped to locally to modules in Julia.

> [@Mark\_Nahabedian](#):
>
> I want there to be something (a local macro) that takes a predicate and logs when the predicate fails before the rule body exits. This could be done with a closure but I want the log message to include the predicate expression and the source location.

I don’t understand why your `@rule` macro couldn’t do this — why can’t it pass the predicate expression and the source location to the closure or whatever data structure you are using?

---

<div class="post-metadata">

**Author:** ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Post date:** [February 4, 2025, 4:55pm UTC](https://discourse.julialang.org/t/local-macros/125539/3 "2025-02-04T16:55:38Z")

</div>

Perhaps I’m not being clear.

I’m proposing to walk the body of the `@rule` macro call and rewrite certain macrocall expressions like `@rejectif`, thus emulating a local macro mechanism.

I was wondering if someone had already implemented such a thing.

I’ve tried using MacroTools for the pattern matching, but the general method on `match_inner` removes line number nodes. I guess I’ll need to write my own matcher.

---

<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:** [February 4, 2025, 7:11pm UTC](https://discourse.julialang.org/t/local-macros/125539/4 "2025-02-04T19:11:28Z")

</div>

I once did this inside out macro which seems related [jkrumbiegel.com – Composing macros inside-out with Julia](https://jkrumbiegel.com/pages/2022-08-09-composing-macros/#the-inside-out-macro)

---

<div class="post-metadata">

**Author:** ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Post date:** [February 14, 2025, 6:42pm UTC](https://discourse.julialang.org/t/local-macros/125539/5 "2025-02-14T18:42:38Z")

</div>

I got this working. [Provide @reject, @rejectif and @continueif within the body of @rule s… · MarkNahabedian/Rete.jl@7f04d7c · GitHub](https://github.com/MarkNahabedian/Rete.jl/commit/7f04d7ce2c7d80a7729547a7c9e08be04831147d).
