@@ -925,41 +925,57 @@ which means that the type of `twelve` is also inferred to be `Int`.
925
925
## Opaque Parameter Types
926
926
927
927
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.
929
930
However, when you write ` some ` in a parameter type
930
931
that's just a shorter syntax for generics, not an opaque type.
931
932
For example,
932
933
both of the functions below are equivalent:
933
934
934
935
``` swift
935
- func drawTwiceSome (_ shape : some Shape ) -> String {
936
+ func drawTwiceGeneric < SomeShape : Shape > (_ shape : SomeShape ) -> String {
936
937
let drawn = shape.draw ()
937
938
return drawn + " \n " + drawn
938
939
}
939
940
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
+ }
941
945
```
942
946
947
+ The ` drawTwiceGeneric(_:) ` function
948
+ uses a generic type parameter named ` SomeShape ` ,
949
+ and explicitly includes the protocol conformance requirement.
943
950
The ` drawTwiceSome(_:) ` function
944
951
uses the type ` some Shape ` for its argument.
945
952
This creates a new, unnamed, generic type parameter for the function
946
953
with a constraint that requires the type to conform to the ` Shape ` protocol.
947
954
Because the generic type doesn't have a name,
948
955
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
956
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:
954
960
955
961
``` swift
956
962
func combine (shape s1 : some Shape, with s2 : some Shape) -> String {
957
963
return s1.draw () + " \n " + s2.draw ()
958
964
}
959
- func combine <S1 : Shape , S2 : Shape >(shape s1 : S1, with s2 : S2) -> String { ... }
965
+
966
+ combine (smallTriangle, trapezoid)
960
967
```
961
968
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,
963
979
use the usual generic syntax instead.
964
980
965
981
<!--
0 commit comments