Last active
November 8, 2023 07:33
-
-
Save tgnivekucn/c141c1cd68d28e8172da588bb21d1813 to your computer and use it in GitHub Desktop.
Opaque type retain type identity example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public protocol Shape { | |
associatedtype T | |
func draw() -> String | |
} | |
class Triangle: Shape { | |
typealias T = Int | |
func draw() -> String { | |
return "Triangle" | |
} | |
} | |
// Boxed type | |
func makeTrapezoid1() -> any Shape { | |
let triangle = Triangle() | |
return triangle | |
} | |
// Opaque type retain type identity example | |
func makeTrapezoid2() -> some Shape { | |
let triangle = Triangle() | |
return triangle | |
} | |
// compile error: use of protocol 'Shape' as a type must be written 'any Shape' | |
func makeTrapezoid3() -> Shape { | |
let triangle = Triangle() | |
return triangle | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment