# How to call macros with module as a "namespace"?

**URL:** <https://forums.swift.org/t/how-to-call-macros-with-module-as-a-namespace/74405>\
**Category:** Macros\
**Created:** [September 6, 2024, 12:56am UTC](https://forums.swift.org/t/how-to-call-macros-with-module-as-a-namespace/74405 "2024-09-06T00:56:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanvoorden](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vanvoorden/32/33619_2.png) [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Post date:** [September 6, 2024, 12:56am UTC](https://forums.swift.org/t/how-to-call-macros-with-module-as-a-namespace/74405/1 "2024-09-06T00:56:03Z")

</div>

Hi! I am wondering if there is any kind of correct syntax for importing a macro package and then calling one of those macros along with the module as a "namespace". For example:

```swift
import MyMacro

let a = 17
let b = 25

let (result, code) = #MyMacro.stringify(a + b)

```

Fails with a `No macro named 'MyMacro'` error.

What happens if I import two macro packages that declare two different macros with the same name? If the two packages declared a type (like a struct or class), I could qualify that type at compile time with `M1.Foo` or `M2.Foo`… but I do not know if there is a way to achieve this with macros.

Is the "best practice" for macro engineers to build macros with non-generic sounding names? Or are we expected to add our macro package name to every macro declared like we did in ObjC days (where `public macro stringify` becomes `public macro MyMacroStringify`)?

Any more ideas or thoughts about that? Thanks!

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [September 6, 2024, 12:58am UTC](https://forums.swift.org/t/how-to-call-macros-with-module-as-a-namespace/74405/2 "2024-09-06T00:58:29Z")

</div>

Unfortunately the language grammar doesn't support invoking freestanding macros with a fully-qualified name; it only parses `'#' + identifier`, not `'#' + MemberAccessExpr`.

I suspect (and hope) that's just an oversight and that there's no fundamental reason it couldn't be allowed. Especially since attached macros can be used that way; `@ModuleName.MacroName` is perfectly valid.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [September 6, 2024, 1:04am UTC](https://forums.swift.org/t/how-to-call-macros-with-module-as-a-namespace/74405/3 "2024-09-06T01:04:18Z")

</div>

I brought this up at the time and it was pointed out that e.g. `#file.count` is valid today and means `(#file).count`. So we’ll need a different syntax, unfortunately.

This is a very similar problem to [Pitch: Fully qualified name syntax](https://forums.swift.org/t/pitch-fully-qualified-name-syntax/28482) and I would love if we end up with a single syntax that can be used for both.

---

<div class="post-metadata">

**Author:** ![beccadax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beccadax/32/34217_2.png) [@beccadax](https://forums.swift.org/u/beccadax)\
**Post date:** [September 6, 2024, 3:31am UTC](https://forums.swift.org/t/how-to-call-macros-with-module-as-a-namespace/74405/4 "2024-09-06T03:31:12Z")

</div>

> [@jrose](#):
>
> This is a very similar problem to [Pitch: Fully qualified name syntax](https://forums.swift.org/t/pitch-fully-qualified-name-syntax/28482) and I would love if we end up with a single syntax that can be used for both.

I expect the syntax I was proposing for that could be extended to cover macros.

---

<div class="post-metadata">

**Author:** ![jlukas](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jlukas/32/317_2.png) [@jlukas](https://forums.swift.org/u/jlukas)\
**Post date:** [September 6, 2024, 6:38am UTC](https://forums.swift.org/t/how-to-call-macros-with-module-as-a-namespace/74405/5 "2024-09-06T06:38:10Z")

</div>

Maybe we could put the module before the `#`, e.g. `MyMacro.#stringify(…)`. I don’t think that would work for `@` macros, though.
