Status of Witness Macros?

It's exciting to see the heavy integration of macros with the new WWDC launches! Can't wait to take everything for a spin.

I was a little surprised to see @Model show up as type attribution instead of a protocol. To me generated functionality through protocols feels a bit more Swifty (such as the existing synthesized Codable or Equatable), though I'm sure there are reasons why a type attribution made more sense in this case.

// i.e. instead of this...
@Model
struct Todo {
    let user: User
    let isComplete: Bool
    let name: String
}

// ...this
struct Todo: Model {
    let user: User
    let isComplete: Bool
    let name: String
}

Regardless - it sounded like witness macros would be the key for adding synthesized functionality through protocol conformance. I know they are mentioned in the vision for macros and briefly discussed in the attached macros review. Are those still planned?

3 Likes

There's a whole lot more going on with @Model than just filling in conformances. For example, it's using member-attribute macros to annotate stored properties with macros that fill in the accessors. Use the "Expand Macro" feature in the Xcode 15 beta to see what's happening under the hood.

For conformances, it's using member macros to generate the witnesses.

We'd like to do witness macros at some point, because they're expected to be better for synthesizing witnesses. They need a bunch of design and implementation work, so I don't know when they'll happen.

Doug

12 Likes

Got it - thanks for the explanation!