Enforce what type a macro can be applied to?

Might be missing something obvious, but is there any way to enforce what types an attached macro can be applied to?

i.e. enforce that the macro can only be applied to type inheriting from a protocol, like the following property wrapper.

struct Foo<C: Collection> {
    var wrappedValue: C

Macro expansion is purely syntactic and so the macro itself can't know anything about type conformance. The best you can do is have the macro expand to something which will fail to type check if the type doesn't have the required conformance, and name the function which will produce a type error something useful to help the user.

1 Like