Hello guys
I'm stuck with one thing, I don't understand a problem I've run into that is really limiting my ability to make reusable code. I have problems casting or unboxing existential types when are inside a Binding, but not when is inside an Optional.
Is there any possible solution to this? I can understand that the opaque type does not get unboxed in this case, but I do not understand why the is and ace do not work as expected.
Can anyone help me with this, please? I appreciate your time
import SwiftUI
protocol Animal {
}
struct Dog: Animal {
}
struct Cat: Animal {
}
var homogeneusArray: [some Animal] = [Dog(), Dog()]
var heterogeneusArray: [any Animal] = [Dog(), Cat()]
var heterogeneusArrayOptionals: [Optional<any Animal>] = [Dog(), Cat()]
var heterogeneusArrayBinding: [Binding<any Animal>] = [.constant(Dog()), .constant(Cat())]
print("type first homogeneusArray: \(type(of: homogeneusArray.first))")
print("type first heterogeneusArray: \(type(of: heterogeneusArray.first))")
print("type first heterogeneusArrayOptionals: \(type(of: heterogeneusArrayOptionals.first))")
print("type first heterogeneusArrayBinding: \(type(of: heterogeneusArrayBinding.first))")
print("")
print("checking type Dog heterogeneusArray (any Animal) -> Dog: \(heterogeneusArray.first is Dog)")
print("checking type any Animal heterogeneusArrayBinding (any Animal): \(heterogeneusArrayBinding.first is Binding<any Animal>)")
print("checking type any Animal heterogeneusArrayBinding (any Animal) -> Dog: \(heterogeneusArrayBinding.first is Binding<Dog>)")
print("checking type any Animal heterogeneusArrayOptionals (any Animal) -> Dog: \(heterogeneusArrayOptionals.first is Optional<Dog>)")