I have a package on GitHub that is already published, I wanted to create a macro as an api for this package.

The question here How can I do that ? Should I build the macro in a separate package ? or can I build it with in the same package ?

and thank you in advance.

Yes, the macro can be implemented within the same package, but a separate module. This module needs to be a dependency of your main module using the macro. You refer to the macro implementation with a macro declaration in the consuming module.

1 Like

So to make sure I understand correctly so this means that I can use the implemented macro with in the package it self, right ?

what do you mean by that ?

Yes, you can use them in the same package.

In the module that uses the macros, you have to declare them first. As an example:

@attached(
    member,
    names: named(foo)
)
macro MyMacro() = #externalMacro(
    module: "MyMacros",
    type: "MyMacroImpl"
)

You basically define a mapping between the module, the implementing type and the macro as it shall be named in your code using it.

@attached can also be @freestanding depending on what you want to use the macro for and how it’s actually implemented.

See also the pretty extensive documentation for macros for more details.

1 Like

Thank you man this really helped a lot. :heart: