Skip to content

Commit 61a6116

Browse files
committed
Expand the outline in the guide
1 parent 8f37584 commit 61a6116

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

TSPL.docc/LanguageGuide/OpaqueTypes.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -924,30 +924,43 @@ which means that the type of `twelve` is also inferred to be `Int`.
924924

925925
## Opaque Parameter Types
926926

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.
927+
In addition to writing `some` to return an opaque type,
928+
you can also write `some` in the type for a parameter.
929+
However, when you write `some` in a parameter type
930+
that's just a shorter syntax for generics, not an opaque type.
931+
For example,
932+
both of the functions below are equivalent:
935933

936934
```swift
937-
func drawTwice(_ shape: some Shape) -> String {
935+
func drawTwiceSome(_ shape: some Shape) -> String {
938936
let drawn = shape.draw()
939937
return drawn + "\n" + drawn
940938
}
939+
940+
func drawTwiceGeneric<SomeShape: Shape>(_ shape: SomeShape) -> String { ... }
941+
```
942+
943+
The `drawTwiceSome(_:)` function
944+
uses the type `some Shape` for its argument.
945+
This creates a new, unnamed, generic type parameter for the function
946+
with a constraint that requires the type to conform to the `Shape` protocol.
947+
Because the generic type doesn't have a name,
948+
you can't refer to that type elsewhere in the function.
949+
The `drawTwiceGeneric(_:)` function
950+
uses a generic type parameter named `SomeShape`,
951+
and explicitly includes the protocol conformance requirement.
952+
953+
XXX Every parameter's type is independent
954+
955+
```swift
941956
func combine(shape s1: some Shape, with s2: some Shape) -> String {
942957
return s1.draw() + "\n" + s2.draw()
943958
}
944-
945-
// The same as:
946-
func drawTwice<SomeShape: Shape>(_ shape: SomeShape) -> String { ... }
947959
func combine<S1: Shape, S2: Shape>(shape s1: S1, with s2: S2) -> String { ... }
948960
```
949961

950-
962+
If you need to multiple generic parameters that are all the same type,
963+
use the usual generic syntax instead.
951964

952965
<!--
953966
This source file is part of the Swift.org open source project

0 commit comments

Comments
 (0)