Skip to content

Commit cbdb274

Browse files
add protocol self conformance exception note
1 parent 53b4ce0 commit cbdb274

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

userdocs/diagnostics/protocol-type-non-conformance.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,28 @@ GenericStruct<AnotherProtocol>().faz()
3939
Constructing the instance of the struct `GenericStruct` with type `AnotherProtocol` will not compile because there is no concrete implementation for the static requirement of the protocol.
4040
There is no implementation for for() used above.
4141

42+
We, however have an exception for `@objc` protocols that conforms to itself as shown below
43+
44+
```swift
45+
import Foundation
46+
47+
@objc protocol SomeProtocol {
48+
func foo()
49+
}
50+
51+
class SomeClass : SomeProtocol {
52+
func foo() {
53+
print("foo called")
54+
}
55+
}
56+
57+
func faz<T : SomeProtocol>(_ t: T) {
58+
t.foo()
59+
}
60+
61+
let c: SomeProtocol = SomeClass()
62+
faz(c)
63+
```
64+
65+
The function `faz` requires that `T` conforms to `SomeProtocol` and we can easily substitute in `SomeProtocol` for `T` because it has no static requirements.
66+

0 commit comments

Comments
 (0)