Skip to content

Commit ab5fba0

Browse files
committed
Mention opaque parameters in the reference
1 parent da90103 commit ab5fba0

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

TSPL.docc/ReferenceManual/Types.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,8 @@ 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-
or the type of a property.
867+
as the type of a parameter to a function, initializer, or subscript,
868+
or as the type of a property.
868869
Opaque types can't appear as part of a tuple type or a generic type,
869870
such as the element type of an array or the wrapped type of an optional.
870871

@@ -909,6 +910,35 @@ that are part of the function's generic type parameters.
909910
For example, a function `someFunction<T>()`
910911
could return a value of type `T` or `Dictionary<String, T>`.
911912

913+
A parameter whose type is an opaque type
914+
is syntactic sugar for a parameter of generic type,
915+
without specifying a name for the generic type parameter.
916+
That is, the following declarations are equivalent,
917+
with `T1` and `T2` representing the unnamed generic type parameters:
918+
919+
```swift
920+
func someFunction(x: some MyProtocol, y: some MyProtocol) { }
921+
func someFunction<T1: MyProtocol, T2: MyProtocol>(x: T1, y: T2) { }
922+
```
923+
924+
Because the generic types doesn't have a name,
925+
there's no way to refer to it in code.
926+
927+
You can't use this syntactic sugar on the type of a variadic parameter.
928+
929+
You can't use an opaque type
930+
as a parameter to a function type being returned,
931+
or as a parameter in a parameter type that's a function type.
932+
In these positions,
933+
the function's caller would have to construct a value
934+
of that unknown type.
935+
936+
```swift
937+
protocol MyProtocol { }
938+
func badFunction() -> (some MyProtocol) -> Void { } // Error
939+
func anotherBadFunction(callback: (some MyProtocol) -> Void) { } // Error
940+
```
941+
912942
> Grammar of an opaque type:
913943
>
914944
> *opaque-type***`some`** *type*

0 commit comments

Comments
 (0)