โญOpaque Types
Swift 5.1 feature
If a function with an opaque return type returns from multiple places, all of the possible return values must have the same type. For a generic function, that return type can use the functionโs generic type parameters, but it must still be a single type. ๐ Swift
the function body determines its concrete return type (including the concrete type of any associated type) at compile time. (๐็ฏไพ โญ๏ธ 5)
use PAT (protocol with associated types) as return types. (๐็ฏไพ โญ๏ธ 5)
preserve type identity (always return the same type). (๐็ฏไพ โญ๏ธ 2, 5)
the
body
of a SwiftUI view returnssome View
. (View is a PAT)
Opaque Type vs. Generic Type
opaque return type: its concrete type is determined by function body ("inside"). - ๐็ฏไพ โญ๏ธ 2, 5
generic type: its placeholders (type parameters) & return type are determined by caller (call site) & type declaration ("outside"). - ๐็ฏไพ โญ๏ธ 1
Opaque Type vs. Protocol Type
opaque type : always refers to ONE specific, concrete type. - ๐็ฏไพ โญ๏ธ 2, 5
protocol type: can refer to MANY (conformaning) concrete types. - ๐็ฏไพ โญ๏ธ 3
Last updated