Sample code which I am experimenting in playground
protocol Shape {
init()
}
class Circle: Shape {
required init() {
}
}
class Square: Shape {
required init() {
}
}
class ShapeMapping<T> {
func map() -> T? {
print("Any shape called")
return nil
}
}
extension ShapeMapping where T: Shape {
func map() -> T? {
print("Shape type called")
return T.self()
}
}
extension ShapeMapping where T: Square {
func map() -> T? {
print("Square type called")
return Square() as? T
}
}
class ShapeWrapper<T> {
func determineShape() -> T? {
return ShapeMapping<T>().map()
}
}
Code works correctly and calls expected method for type Square when I call
let square: Square? = ShapeMapping<Square>().map()
Output: Square type called
But, when I call a wrapper method and pass Square type output is different
let mappingSquareShape: Square? = ShapeWrapper<Square>().determineShape()
Actual: Any shape called
Expected: Square type called
Is type information does not get passed from ShapeWrapper to ShapeMapping? Any idea what's going on here?