Skip to content

Instantly share code, notes, and snippets.

@tgnivekucn
Last active November 8, 2023 07:33
Show Gist options
  • Save tgnivekucn/c141c1cd68d28e8172da588bb21d1813 to your computer and use it in GitHub Desktop.
Save tgnivekucn/c141c1cd68d28e8172da588bb21d1813 to your computer and use it in GitHub Desktop.
Opaque type retain type identity example
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