Trying to understand why my Xcode-beta 2 is not able to understand the underlying type for the following example
/// Protocol that has the idea of draw
protocol Drawable {
func draw()
}
/// Implementation of a circle which is drawable
struct Circle: Drawable {
func draw() {
print("Circle")
}
}
/// Implementation of a Triangle which is drawable
struct Triangle: Drawable {
func draw() {
print("Triangle")
}
}
/// Let make an instance as shown in the example in the `Embrace Swift Generics` Talk
func underlyingTypeDoesNotChangeWithinTheScop() {
var drawable: some Drawable = Circle()
drawable.draw()
drawable = Circle() /// 👈🏽 This line should compile, since the underlying type does not change within the scope
drawable.draw()
}
Why am I getting the following error cannot assign value of type 'Circle' to type 'some Drawable' when I try to assign Circle to var drawable, from what I understand the underlying type remains the same within the scope.
Is this an Xcode14-beta 2 issue?
I got an answer form my FB. That is a present Swift limitation, maybe will improve in the future.
Thanks for your report. We have additional information on the Swift issue.
The issue here is a value of opaque result type from one location cannot be used as an opaque result type in another location because the underlying type erased to some Drawable could be different.
For example, getCircle() could return SpecialCircle which is going to be different from Circle originally assigned to drawable and that would mean that drawable could behave differently before and after assignment which is incorrect.
We are still working on this on our side, and we will follow up again.