Skip to content

Commit c51f443

Browse files
committed
Fill in the discussion in the guide
- Start with the generic syntax, since that's what readers have seen in previous chapters already. - Walk through an example of using 'some' more than once. - Match func/subscript/init order in prose to chapter ordering.
1 parent 61a6116 commit c51f443

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

TSPL.docc/LanguageGuide/OpaqueTypes.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -925,41 +925,57 @@ which means that the type of `twelve` is also inferred to be `Int`.
925925
## Opaque Parameter Types
926926

927927
In addition to writing `some` to return an opaque type,
928-
you can also write `some` in the type for a parameter.
928+
you can also write `some` in the type for a parameter
929+
to a function, subscript, or initializer.
929930
However, when you write `some` in a parameter type
930931
that's just a shorter syntax for generics, not an opaque type.
931932
For example,
932933
both of the functions below are equivalent:
933934

934935
```swift
935-
func drawTwiceSome(_ shape: some Shape) -> String {
936+
func drawTwiceGeneric<SomeShape: Shape>(_ shape: SomeShape) -> String {
936937
let drawn = shape.draw()
937938
return drawn + "\n" + drawn
938939
}
939940

940-
func drawTwiceGeneric<SomeShape: Shape>(_ shape: SomeShape) -> String { ... }
941+
func drawTwiceSome(_ shape: some Shape) -> String {
942+
let drawn = shape.draw()
943+
return drawn + "\n" + drawn
944+
}
941945
```
942946

947+
The `drawTwiceGeneric(_:)` function
948+
uses a generic type parameter named `SomeShape`,
949+
and explicitly includes the protocol conformance requirement.
943950
The `drawTwiceSome(_:)` function
944951
uses the type `some Shape` for its argument.
945952
This creates a new, unnamed, generic type parameter for the function
946953
with a constraint that requires the type to conform to the `Shape` protocol.
947954
Because the generic type doesn't have a name,
948955
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.
952956

953-
XXX Every parameter's type is independent
957+
If you write `some` before more than one parameter's type,
958+
each of the generic types are independent.
959+
For example:
954960

955961
```swift
956962
func combine(shape s1: some Shape, with s2: some Shape) -> String {
957963
return s1.draw() + "\n" + s2.draw()
958964
}
959-
func combine<S1: Shape, S2: Shape>(shape s1: S1, with s2: S2) -> String { ... }
965+
966+
combine(smallTriangle, trapezoid)
960967
```
961968

962-
If you need to multiple generic parameters that are all the same type,
969+
In the `combine(shape:with:)` function,
970+
the types of the first and second parameter
971+
must both conform to the `Shape` protocol,
972+
but there's no constraint that requires them to be the same type.
973+
When you call `combine(shape:with)`,
974+
you can pass two different shapes ---
975+
in this case, one triangle and one trapezoid.
976+
If you need to write a function
977+
that has multiple generic parameters
978+
and constrain them to be are all the same type,
963979
use the usual generic syntax instead.
964980

965981
<!--

TSPL.docc/ReferenceManual/Types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ that conforms to a protocol or protocol composition,
864864
without specifying the underlying concrete type.
865865

866866
Opaque types appear as the return type of a function or subscript,
867-
as the type of a parameter to a function, initializer, or subscript,
867+
as the type of a parameter to a function, subscript, or initializer,
868868
or as the type of a property.
869869
Opaque types can't appear as part of a tuple type or a generic type,
870870
such as the element type of an array or the wrapped type of an optional.

0 commit comments

Comments
 (0)