I want to do this:
var arr = [0,1,0,1,0,2,1,0,0]
switch arr {
case [1,_,_,_,_,_,_,_]: print("Yay")
default: print("Nay")
}
But I get this error: "'_' can only appear in a pattern or on the left side of an assignment"
I searched around on this error message and "wildcards swift array switch statements" and it seems like it is not possible to use wildcards in this context, but there may be some sort of workaround using 'operator overloading' with something involving ~=. However, this is way over my current knowledge, I haven't found a case example exactly like mine, and there seems to be a fair amount of implied concern that operator overloading is bad for some reason. Also, the examples I've found that is sort of like my case look very convoluted. I'm a self-taught amateur just trying to hack together a basic app to make my work life easier, so apologies for any stupid questions but I'd like to better understand the following:
Why can't I use wildcards in this context? It seems like they work with tuples but not arrays for some reason, and I don't get why one would work but not the other.
Is operating overloading actually 'bad' and is there a relatively straightforward way to implement this functionality?
If not- can someone recommend an alternate approach? I chose this approach because I have two arrays, one for a series of questions, one to record responses to each question. I want to implement pretty complicated logic where the configuration of responses to prior questions will determine which question is presented next (example below). I wasted a bunch of time drowning in convoluted 'if' statements before realizing I needed a cleaner approach and this seemed to be it. However, without wildcards or something similar, it would not be practical to explicitly define every case. Could someone recommend a different approach in this situation? Obviously, this is a streamlined example and there would be many more defined cases. Thanks in advance.
var responseArr = [0,2,1,2,1,0,0] // 0 = unasked, 1 = asked answered no, 2 = asked answered yes switch responseArr { case: [0,_,_,2,1,0,_]: nextQuestion = questionArr[0] // If 'yes' to question 4, no to question 5, and questions 1 and 6 were not asked yet, ask question 1 case: [1,_,_,2,1,0,_]: nextQuestion = questionArr[5] // If 'yes' to question 4, no to question 5, no to question 1, and question 6 was not asked yet, ask question 6 ... default: print("error") }