I don't if I'm doing this the right way.
When switching on a enum with an associated value represented as an IndexSet, the code crashes at runtime.
import Foundation
enum Action {
case a
case b(IndexSet)
}
func doSomething(_ action: Action) {
switch action {
case .a:
print("Perform logic when a")
case .b(_):
print("Delete entries when b using indexSet")
}
}
doSomething(.a)
But it works perfectly with other types, String or Int for instance.
import Foundation
enum Action {
case a
case b(String)
}
func doSomething(_ action: Action) {
switch action {
case .a:
print("Perform logic when a")
case .b(_):
print("Perform logic when b using String")
}
}
doSomething(.a)
enum E {
case a
// case b(IndexSet)
case c(Int)
}
func moo(e: E) {
switch e {
case .a: print("a")
// case .b: print("b")
case .c: print("c")
}
}
moo(e: .a)
//moo(e: .b(IndexSet()))
moo(e: .c(4))
The output is as-expected:
a
c
Uncomment all references to case b except the call to moo(e: b(IndexSet())) and the output becomes:
Thank you for you answer!
Wow the double a as output was unexpected.
So the compiler compiler considers case a as the right match even when we explicitly provide case c as input to the function?
So I wasn't doing anything weird then.
Is IndexSet the culprit?
Does it mean I have to find a work around?
Given that I need to use IndexSet inside my enum?
Or maybe I'm completely missing the point.
It appears that the compiler can't handle having IndexSet as an associated value. I just found this manifestation while researching the crash you reported. The crash also reproduces if you uncomment the middle call to moo(e:).