Does nature of how current macros work allow for thinking about "aggregate" macro? By "aggregate" I mean a design of macro that expands based on a group of annotations attached to arbitrary declarations. The implementation method passes an argument with list of declaration syntaxes that were annotated with same id
.
Some use cases could be raising access levels for certain enum cases.
enum _Foo {
case a
@aggregate("Foo")
case b
}
#publicEnum(id: "Foo")
This publicEnum
freestanding macro would expand (as implemented) as
public enum Foo {
case b
}
Perhaps similar could be done with Member macro and some marker property wrapper, this is just for simple example. Further,
enum Foo2 {
@aggregate("Foo")
case c
}
would be allowed and the expansion would result based on both existing annotations in
public enum Foo {
case b
case c
}
Apparently, handling name collisions, supported declaration kinds, etc... is responsibility of macro implementation.
The real use case that I'm interested in is what was previously discussed here as sealed protocols.
With combination of this and currently existing macros, I imagine I could do this:
#sealed("MyProt")
@MyProt struct A {}
@MyProt struct B {}
where MyProt
macro adds also @aggregate("MyProt")
and conformance and these expand to
protocol MyProt {
var sealed: MyProtSeal { get }
}
enum MyProtSeal {
case a(A)
case b(B)
}
@aggregate("MyProt") struct A {}
extension A: MyProt {
var sealed: MyProtSeal { .a(self) }
}
@aggregate("MyProt") struct B {}
extension B: MyProt {
var sealed: MyProtSeal { .b(self) }
}
Take it with a grain of salt, as a meta pitch, just thinking about where this could go. I only got to learn macros now and I'm hyped. Could we once have a macro with a role that allows it to use group of pieces of code from arbitrary locations?