You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: TSPL.docc/LanguageGuide/OpaqueTypes.md
+24-1Lines changed: 24 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -498,7 +498,7 @@ A boxed protocol type is also sometimes called an *existential type*,
498
498
which comes from the phrase
499
499
"there exists a type *T* such that *T* conforms to the protocol".
500
500
To make a boxed protocol type,
501
-
write `any` before the name of a protocol.
501
+
write `any` before the name of a protocol (or protocol composition).
502
502
Here's an example:
503
503
504
504
```swift
@@ -621,6 +621,29 @@ if let downcastTriangle = vertical.shapes[0] as? Triangle {
621
621
622
622
For more information, see <doc:TypeCasting#Downcasting>.
623
623
624
+
### Existential Of Protocol Composition
625
+
626
+
An existential can also creating by writing `any` before a protocol composition type (see <doc:Protocols#Protocol-Composition>). This creates a box which holds a structure, class or enum that conforms to all the protocols listed.
627
+
628
+
```
629
+
protocol Named {
630
+
var name: String { get }
631
+
}
632
+
protocol Aged {
633
+
var age: Int { get }
634
+
}
635
+
struct Person: Named, Aged {
636
+
var name: String
637
+
var age: Int
638
+
}
639
+
struct Dog: Named, Aged {
640
+
var name: String
641
+
var age: Int
642
+
var breed: String
643
+
}
644
+
var family: [any (Name & Aged)]
645
+
```
646
+
624
647
## Differences Between Opaque Types and Boxed Protocol Types
0 commit comments