How do you incorporate a protocol's static members into another type's extensions by way of a generic placeholder?

If a protocol, represented in a generic placeholder (ModifiedCircle.Modifier, here) has static members, and you want to use them in an extension to another type, is there any better option than passing along the metatype?

SwiftUI’s modifier-heavy approach is the biggest obvious use case for this, but it’s not at all specific to SwiftUI.

struct ModifiedCircle<Modifier: Circle.Modifier>: View {
  var body: some View {
    Circle().modified(Modifier.self) // Can you improve this?
  }
}
extension Circle {
  protocol Modifier {
    associatedtype Modified: View
    static func modify(_ circle: Circle) -> Modified
  }

  func modified<Modifier: Circle.Modifier>(_: Modifier.Type) -> some View {
    Modifier.modify(self)
  }
}

Not really, as far as I know. The only type information that can be passed to and inferred for Circle.modified() is going to be through either its parameters or return type.

2 Likes

If this get landed someday, we can then avoid spelling out the .self part.

2 Likes