UPDATE: See the first comment by @tera about what the issue really is. Changed the topic title from "Loosing type info in enums with associated values" to "Loosing type info depending on iteration mode".
In the following code with the enum case indirect case sequence(any Sequence<MyEnumeration>) the MyEnumeration type seems to "get lost" (see case .sequence(let sequence). Why is this the case?
enum MyEnumeration {
case number(Int)
case text(String)
indirect case array([MyEnumeration])
indirect case sequence(any Sequence<MyEnumeration>)
}
let sequence: (any Sequence<MyEnumeration>)? = nil
sequence?.forEach { value in
doSomething(with: value) // OK, value is of type MyEnumeration
}
func doSomething(with values: MyEnumeration...) {
values.forEach { value in
switch value {
case .number(let number):
print("number \(number)")
case .text(let text):
print("text \(text)")
case .array(let array):
for value in array { doSomething(with: value) }
case .sequence(let sequence):
for value in sequence { doSomething(with: value) } // error: "Cannot convert value of type 'Any' to expected argument type 'MyEnumeration'"
}
}
}
You have forEach in the first place and for in the second place. The quintessential example:
var values: any Sequence<Int> = [1, 2, 3]
values.forEach { value in
let x: Int = value // ok, unused variable warning
}
for value in values {
let x: Int = value // š Cannot convert value of type 'Any' to specified type 'Int'
}
There were some explanations above about what hasnāt been implemented yet so it cannot work yet, but from a userās standpoint it just seems that āsomething goes wrongā, so even if it is not really a bug, it seems at least like a major omission. Therefore I filed a bug report (using some of the code of @tera above).