. (, , ) - 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())
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.