Skip to content

Commit da90103

Browse files
committed
Outline what 'some' means in parameters
1 parent cc24ca4 commit da90103

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

TSPL.docc/LanguageGuide/OpaqueTypes.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,34 @@ which means that the type of `twelve` is also inferred to be `Int`.
921921
}
922922
-->
923923

924+
925+
## Opaque Parameter Types
926+
927+
XXX Outline:
928+
929+
- You can write `some` as the type for a parameter
930+
to a function, initializer, or subscript.
931+
- In that usage, it's syntactic sugar for a generic.
932+
- You can't refer to the generic type -- because it has no name.
933+
- If you write `some P` more than once,
934+
they each refer to a different type.
935+
936+
```swift
937+
func drawTwice(_ shape: some Shape) -> String {
938+
let drawn = shape.draw()
939+
return drawn + "\n" + drawn
940+
}
941+
func combine(shape s1: some Shape, with s2: some Shape) -> String {
942+
return s1.draw() + "\n" + s2.draw()
943+
}
944+
945+
// The same as:
946+
func drawTwice<SomeShape: Shape>(_ shape: SomeShape) -> String { ... }
947+
func combine<S1: Shape, S2: Shape>(shape s1: S1, with s2: S2) -> String { ... }
948+
```
949+
950+
951+
924952
<!--
925953
This source file is part of the Swift.org open source project
926954

0 commit comments

Comments
 (0)