[Accepted] SE-0402: Generalize `conformance` macros as `extension` macros

Hello, everyone. The review of SE-0402 ran from July 3rd to the 17th. Overall feedback was light but positive. Comments on the review were mostly limited to questions about the behavior, some of which may lead to future work, but the Language Steering Group feels that the proposal represents progress as it is and doesn't need to be held up. Accordingly, SE-0402 has been accepted.

As always, I'd like to thank the community for participating in the evolution process.

John McCall
Review Manager

10 Likes

Was there any discussion regarding this point?

I believe that accepting the proposal as is would block this possibility from being a future direction, because the conformance names are currently listed directly under conformances instead of being embedded in named(…) as it is for names.

I believe the idea is that you could write prefixed(Foo) in the conformances clause; I don’t see why that would be ruled out.

If this is technically no issue, then it will suffice. Still, I believe this could be a rough edge when the two APIs work differently.

Thank you for the improvement. It looks much better now.

However I found that it became impossible to generate arbitrary conformance now that is required inplace or conformance to some protocol(s) that are not defined by the moment of defining macro.

For example, previously it could be used in some situations, e.g.
Marco definition:

@attached(conformance)
@attached(peer, names: arbitrary)
public macro GenProtocolAndConformance(_: String) = #externalMacro(...)

That could expand the following code:

@GenProtocolAndConformance("MyProtocol")
public struct MyStruct {
    var var1: Int
    
    func foo() {
        // do stuf
    }
    // ...
}

To:

public struct MyStruct {
    var var1: Int
    
    func foo() {
        // do stuf
    }
    // ...
}

// Expands to
protocol MyProtocol {
    var var1: Int
    
    func foo()
}

extension MyStruct : MyProtocol {}

If I understand correctly, now it is impossible. I've tried to do it with both: extension and peer macro. With extension I can't define unknown protocol and with peer macro I have the following error:

error: macro expansion cannot introduce extension

Not sure if it should be addressed here (please let me know if this is a wrong place).

1 Like