Search code examples
swiftwarningsprotocols

Swift "nearly matches defaulted requirement"


I have a protocol like this, with some default implementation for the methods:

public protocol BusinessModelTransformable {
    func modelId() -> ModelID
    func asModelElement(bModel: BusinessModel) -> ModelElement
}
public extension BusinessModelTransformable {
    func modelId() -> ModelID { UUID().uuidString }
    func asModelElement(bModel: BusinessModel) -> ModelElement {
        ModelElement(bModel: bModel, id: self.modelId())
    }
}

In another file (of another package if that matters), I do add a conformance to the protocol to a struct, but I don't want the default implement, so I provide my own:

extension ImpDesc: BusinessModelTransformable {
    public func modelId() -> ModelID { self.id.uuidString }
    public func asModelElement(bModel: BusinessModel) -> ModelElement {
        ExtendedModelElement(bModel:    bModel,
                             id:        self.id.uuidString,
                             payload:   self)
    }
}

I get this warning:

Instance method 'asModelElement(bModel:)' nearly matches defaulted requirement 'asModelElement(bModel:)' of protocol 'BusinessModelTransformable'

only for the asModelElement method.

  • Why this warning, as this is to me a perfectly valid pattern ?
  • Why only for the second method ?
  • how to get rid of that warning () ?

Solution

  • As written by @Joakim Danielson in his comment (many thanks): "while the declarations look the same there is some difference between them", there was indeed a name conflict that was leading to the problem.

    Lessons learnt, when the compiler tells you something, that might very well be true.