Should conditional conformances able to make this work, in theory?

struct Handler {}

protocol HandlerWire {
  var handler: Handler { get }
}

protocol InteractorWire {
  associatedtype Interactor : HandlerWire
  var interactor: Interactor { get }
}

class BaseModerator {}

// Trailing 'where' clause for extension of non-generic type 'BaseModerator'
extension BaseModerator : HandlerWire where Self : InteractorWire {
  var handler: Handler {
    return interactor.handler
  }
}

All I want to express here is, please conform to HandlerWire and provide a default implementation for var handler: Handler { get } on the subclass if that subclass also conforms to InteractorWire.

Can't you do protocol InteractorWire: HandlerWire directly, with handler implementation in an extension?

1 Like

Silly me, that would work in my situation because all Interactor types conform to HandlerWire. Not sure why I over engineered it here.


Although the issue is solved for me I'm still curious if the extension still should work?!

My reasoning is that conditional conformances are great for dynamic dispatch, but I yet do not see a worthy reason to check for something that is known at compile time in this manner – whether BaseModerator conforms to InteractorWire. A similar flow would be "if I declared a variable named x over there, do ...". But the point is you know whether you declared that variable or you can otherwise go and check and act accordingly. Whenever you use BaseModerator, it either conforms of not without depending on generic parameters. And retroactive conformances are handled separately as far as I understand.

It's kind of an allusion as well, since a non-protocol type is its own Self.