switch-statement

Is there any design pattern to avoid a nested switch case?

杀马特。学长 韩版系。学妹 提交于 2019-12-22 10:58:54
问题 I have seen similar kind of threads, But, not sure how to exactly apply the solutions to my case. My problem is that i have a set of usecases lets say 'A','B','C',There are certain commands i need to execute when the input passed(2 usecases are the input) is any 2 of the listed usecases. for example: switch(input1) { case A: break; case B: break; case C: break; } inside the each case, i will have to check on input 2, so, the final code could look like switch(input1) { case A: { switch(input2)

Functional switch case with object literal and Typescript

好久不见. 提交于 2019-12-22 10:39:44
问题 So I've got this classic switch case redux reducer in a todomvc that I want to make functional but can't seem to wrap my head around ts typings for that. Switch case works great for pattern matching and narrows down action discriminated union by type. But I don't seem to get how to pass around narrowed actions with a functional approach where object literal's key should do a type narrowing. What I got so far is union type of all functions and some ts errors by the way. Would really appreciate

Swift Pattern match on Array<Any>

泪湿孤枕 提交于 2019-12-22 10:15:19
问题 Swift 1.2 I'm trying to pattern match in a switch case in a function that take a type Any as it's parameter, in order to dispatch to a private more specialize init. Here is a Playground extrapolation : import Foundation struct myStruct { } func switchOnAny(any: Any) -> String { println("Dynamic Type == \(any.dynamicType)") switch any { case let array as [Any]: return "Array" case let array as NSArray: return "NSArray" default: return "Default" } } let emptyStringArray : [String] = [] let

is there a way in PHP to restart a loop in a foreach, or change the test value in a switch?

若如初见. 提交于 2019-12-22 10:05:31
问题 if i'm looping over an array, and while in the middle of one of the loops i discover some small issue, change ...something..., and need to try again ... is there a way to jump back to the top of the loop without grabbing the next value out of the array? i doubt this exists, but it would be some keyword like continue or break . in fact, it would be a lot like continue , except that it doesn't get the next item, it maintains what it has in memory. if nothing exists, can i insert something into

Should this (Generic Range Class) Conditional logic be replaced with Polymorphism? If yes, then why?

假装没事ソ 提交于 2019-12-22 10:02:59
问题 Shown below is a Generic Range class. The purpose of this is to save a Range and then later when requested specify (boolean) if a given value is in the range. I have read multiple posts, questions, blogs etc. that say "Replace Conditional with Polymorphism" My question, is it really worth separating the code out into multiple classes where each class would literally have one line of code. Hopefully the code below will show what I mean. The class is dependent on two more classes, which are not

Need to know switch performance C#

我与影子孤独终老i 提交于 2019-12-22 08:55:51
问题 Suppose there is a switch on some method like static string switchExample(string abc){ switch(abc.ToUpper()) { case "123": return "Numeric"; case "ab": return "Alphabets"; default: return "symbol"; } } Now here I need to know if there are any performance issues to use multiple points of exit(return on each case) like I did in given code rather to pass updated value at one time by making some temp string at the start and filling it with corresponding case match. 回答1: Now here i need to no is

Switch case weird scoping

我的未来我决定 提交于 2019-12-22 08:27:05
问题 Reviewing some 3rd party C code I came across something like: switch (state) { case 0: if (c=='A') { // open brace // code... break; // brace not closed! case 1: // code... break; } // close brace! case 2: // code... break; } Which in the code I was reviewing appeared to be just a typo but I was surprised that it compiled with out error. Why is this valid C? What is the effect on the execution of this code compared to closing the brace at the expected place? Is there any case where this could

Can the switch statement have more than one variable?

女生的网名这么多〃 提交于 2019-12-22 05:37:17
问题 The question is based in all the languages that use the switch block (C++, C# PHP , Java , Python and so on). So is it possible to have something like this? switch (var1, var2) case var1 = a: something break; case var2 = b: something break; case var1 = 0 ,var2 = 1 etc... 回答1: Yes/No. No traditional language with a "switch" statement has this. It does exist in something called "pattern matching". C#, Java, PHP, and Python do not support pattern matching (not completely sure about PHP, but I

Is It Possible To Do The Following In A Switch Statement - C++?

折月煮酒 提交于 2019-12-22 04:49:11
问题 I am a programming student in my second OOP class, and I have a simple question that I have not been able to find the answer to on the internet, if it's out there, I apologize. My question is this: Is it possible have Boolean conditions in switch statements? Example: switch(userInputtedInt) { case >= someNum && <= someOtherNum break; // Is this possible? } 回答1: No this is not possible in C++. Switch statements only support integers and characters (they will be replaced by their ASCII values)

How to make gcc/clang warn about missing breaks in switch statements

拈花ヽ惹草 提交于 2019-12-22 04:49:09
问题 Is there any way to make gcc or clang warn about missing breaks in switch statements? Specifically, I almost always want case statements to end with breaks, and it would be great it I could get the compiler to complain if I don't. Even better would be if it would look for either a break statement or a "// fall through" comment. Is there a different solution people use to help themselves not screw this up? 回答1: With Clang trunk, use -Wimplicit-fallthrough . If you're using C++11, intentional