Can Macros add an attribute to a type?

I'm have a use case that I'm not sure if macros can cover in their current form:

  1. Can an attached peer macro add an attribute to their enclosing type?
struct SomeType {
  @MyMacro
  let member: Int
}

should get transformed into

@Attribute
struct SomeType {
  let member: Int
}

@Attribute could be @objc or @dynamicMemberLookup for example

  1. If a macro on a member can't touch the enclosing type; still, is there a way to add an attribute to a type?
@MyMacro
struct SomeType { ... }

to

@dynamicMemberLookup
struct SomeType { ... }

No, this is not possible, and won't be. Macros can augment programs from the outside-in, not inside-out, so a macro on a member cannot affect its context.

This is not possible now, but it's something we could consider adding in the future. A macro on a type or extension can add attributes to its members (which is outside-in).

Doug

3 Likes

Thanks for the reply Doug.
I will work with what's available now, just playing around trying to see how far we can get with macros ^^

+1 on this, we should be able to add attributes to a type or even preprocessor macros to enclose a type with a certain flag for example.