
2
2
.
.
5
5
.
.
6
6
O
O
p
p
a
a
q
q
u
u
e
e
R
R
e
e
t
t
u
u
r
r
n
n
T
T
y
y
p
p
e
e
(
(
s
s
o
o
m
m
e
e
P
P
r
r
o
o
t
t
o
o
c
c
o
o
l
l
)
)
Opaque Return Type is when Function returns some Protocol.
It is called Opaque because caller doesn't know which specific Concrete Type (that implements specified Protocol) is
always going to be returned by the Function.
Always return the same Concrete Type
If Function returns Opaque Type it will always return the same Concrete Type that implements specified Protocol.
Which Concrete Type depends on Function's implementation (it is nowhere explicitly declared).
But Compiler can check your code to make sure that you are always returning the same Concrete Type as intended.
Return Protocols that use associatedtype
Protocols that use associatedtype can't be used as Function return value (because Compiler doesn't know exact type).
But you can add keyword some to create Opaque Return Type because then Compiler will infer which concrete type that
implements that Protocol is going to be returned every time. It is as if Function returns Concrete Type but which one
specifically is defined by Function Implementation rather then explicitly declaring it as Function's return Type.
P
P
r
r
o
o
t
t
o
o
c
c
o
o
l
l
v
v
s
s
s
s
o
o
m
m
e
e
P
P
r
r
o
o
t
t
o
o
c
c
o
o
l
l
If Function returns
● Protocol (Animal) it means it can return any Type that implements that Protocol (Dog, Cat).
First time you call Function it might return instance of Dog Class, and second time it might return instance of Cat Class.
● some Protocol (some Animal) it means it will always return the same Type that implements Protocol.
Every time you call the Function it will always return instance of a Dog Class (which is more restrictive).
So if you accidently write your Function so that it might return both Dog or Cat you will get Compiler error.
G
G
e
e
n
n
e
e
r
r
i
i
c
c
v
v
s
s
O
O
p
p
a
a
q
q
u
u
e
e
T
T
y
y
p
p
e
e
You can think of Opaque Return Type as being the reverse of a Generic Type
● Generic Type allows caller to define concrete type of function parameters & return value.
Type is abstracted away from the function implementation (Function doesn't know which type will be used).
● Opaque Return Type allows Function to define which concrete type will be returned.
Type is abstracted away from the caller (caller doesn't know which concrete type is always being returned).
Content
Example: Protocol vs some Protocol
Example: associatedtype