Cannot match protocol by creating extension using Where Clauses

protocol Typographable {
    func setTypography(_ typography: Typography)
}

extension UILabel: Typographable {}

extension Typographable where Self == UILabel {

    func setTypography(_ typography: Typography) {

        self.font = typography.font
        self.textColor = typography.textColor
        self.textAlignment = typography.textAlignment
        self.numberOfLines = typography.numberOfLines
    }
}

I created a protocol Typographable, UILabelimplement this protocol, and implementation - in extension Typographable where Self == UILabel.

it works fine in swift 4.0, but it no longer works in swift 4.1, error message Type 'UILabel' does not conform to protocol 'Typographable'

I carefully read CHANGELOG in swift 4.1, but I can not find anything useful.

Is this normal, am I missing something?

+4
source share
1 answer

. (, , ) - effec t # 12174, , Self , , 4.1:

protocol P {
  init()
  static func f() -> Self
}

extension P {
  static func f() -> Self {
    return self.init()
  }
}

class C : P {
  required init() {}
}

Swift 4.0.3 f():

'f()' 'C' Self P '

? , :

class C {}
class D : C {}

protocol P {
  func copy() -> Self
}

extension P where Self == C {
  func copy() -> C {
    return C()
  }
}

extension C : P {}

let d: P = D()
print(d.copy()) // C (assuming we could actually compile it)

Swift copy() , C D, . Swift 4.1 ( ), , Self .

, , , Self C, , .

:

protocol Typographable {
    func setTypography(_ typography: Typography)
}

extension UILabel: Typographable {}

extension Typographable where Self : UILabel {

    func setTypography(_ typography: Typography) {

        self.font = typography.font
        self.textColor = typography.textColor
        self.textAlignment = typography.textAlignment
        self.numberOfLines = typography.numberOfLines
    }
}

, , Swift 4.1. , , :

protocol Typographable {
    func setTypography(_ typography: Typography)
}

extension UILabel : Typographable {

    func setTypography(_ typography: Typography) {

        self.font = typography.font
        self.textColor = typography.textColor
        self.textAlignment = typography.textAlignment
        self.numberOfLines = typography.numberOfLines
    }
}

, # 12174 Self ( ) . , thunk, .

, :

class C {}

protocol P {
  func foo()
}

extension P {
  func foo() {}
}

extension C : P {}

Swift 4.0.3, C ( PWT , ) :

(C) -> Void

( , , , thunks, , PWT , . , , )

Swift 4.1 thunk :

<Self : C>(Self) -> Void

? Self, ( ).

, :

extension P where Self == C {
  func foo() {}
}

, (C) -> Void thunk, <Self : C>(Self) -> Void. , (, , Self C, , ).

, , :

extension P where Self : C {
  func foo() {}
}

, <Self : C>(Self) -> Void.

# 12174, , , . , :

class C {}

protocol P {
  associatedtype T
  func foo() -> T
}

extension P where Self == C {
  func foo() {} // T is inferred to be Void for C.
}

extension C : P {}

, , . where Self : C.

+3

Source: https://habr.com/ru/post/1696096/


All Articles