filimo
(VictorK)
July 31, 2019, 8:40am
1
Why does the compiler throw this error for returnShapes2?
protocol Shape {}
struct Circle: Shape { }
struct Rectangle: Shape { }
func returnShapes() -> Shape { Bool.random() ? Circle() : Rectangle() }
// Error: Cannot convert return expression of type "Circle" to return type "some Shape"
func returnShapes2() -> some Shape { Bool.random() ? Circle() : Rectangle() }
func returnShapes3() -> some Shape { Circle() }
lukasa
(Cory Benfield)
July 31, 2019, 9:16am
2
This is a restriction of opaque result types: while the caller does not see what the concrete return type of the method is, the method is still required to return only one type on all paths. In the case of returnShapes2
, you may return either Circle
or Rectangle
, which are different concrete types. That is not allowed.
To do this you'd need to use type erasure via an AnyShape
structure.
2 Likes
filimo
(VictorK)
July 31, 2019, 10:17am
3
I see, I have to return AnyShape with Circle/Rectange
protocol Shape {
associatedtype S
}
extension Shape {
typealias S = Self
}
struct Circle: Shape { }
struct Rectangle: Shape {}
struct AnyShape: Shape {
init<S: Shape>(_ shape: S) { }
}
let shape = Bool.random() ? AnyShape(Circle()) : AnyShape(Rectangle())
1 Like
young
(rtSwift)
July 31, 2019, 4:37pm
4
May I ask what's the purpose of doing this elaborate type setup?
mayoff
(Rob Mayoff)
July 31, 2019, 5:37pm
5
It's probably related to this earlier question .
1 Like
filimo
(VictorK)
July 31, 2019, 5:45pm
6
I tried to figure out how to use the protocol, associate type and some keyword together.