Path: utzoo!attcan!uunet!husc6!rutgers!att!ihlpb!nevin1 From: nevin1@ihlpb.ATT.COM (Liber) Newsgroups: comp.lang.c++ Subject: Re: Improved switch statement Message-ID: <9245@ihlpb.ATT.COM> Date: 22 Dec 88 01:21:42 GMT References: <6590072@hplsla.HP.COM> <1106@etive.ed.ac.uk> <33528@bbn.COM> <1907@ogccse.ogc.edu> <574@redsox.UUCP> Reply-To: nevin1@ihlpb.UUCP (55528-Liber,N.J.) Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 86 In article <574@redsox.UUCP> campbell@redsox.UUCP (Larry Campbell) writes: |I think it would be eminently sensible for the switch statement to operate |on any type that has an == operator defined; of course the case labels would |have to be the correct type (or coerced thereto). You have to be far more restrictive than that. Not only must operator== be defined, but it must be defined to return a scalar (since the controlling expression of an if statement must have scalar type). Note: you can't easily use implicit type conversion, since there is no way of telling which of the scalar types (pointer, float, int, etc.) the return value should meaningfully be converted to. Also, it makes a big difference on whether switch (x) { case y: ... } is translated to if ((x) == (y)) or if ((y) == (x)) since there is no guarantee by the language (nor should there be, even if it were feasable) that operator== is commutative with respect to it's arguments, even if the arguments are the same type (let alone if the arguments are of different types). In effect, operator== must *semantically* mean 'equal-to', in order for this extension to switch to not appear to be kludgey. This proposal doesn't fit in with the rest of the language. |A common and useful idiom I learned in BLISS which is, unfortunately, not |possible in C or C++ today, is the following code fragment (transliterated |from BLISS into pseudo-C), which checks a number of boolean flags: | bool flag1, flag2, flag3; | switch (TRUE) | { | case flag1: | // stuff | case flag2: | // stuff | case flag3: | // stuff | default: | // whatever | } |I think this is much clearer and neater and far less prone to error during |maintenance than the current alternative: | bool flag1, flag2, flag3; | if (flag1) | then | // stuff | else | if (flag2) | then | // stuff | else | if (flag3) | then | // stuff | else | // whatever I might agree, except that your pseudo-C does not translate into the if-then-else chain that you wrote here. You have assumed that each of the cases has an implicit "break" statement, which is something you cannot do in the generalized extension (if you don't want the extension to look kludgy, that is). What do you plan on doing about fallthrough? Although you can do it using temp variables as flags, the resultant code will probably be less efficient than hand-coding the if-then-else chain (your flow of control would probably be different, since you weren't trying to squeeze it in to the switch statement format). IMHO, this proposal is very kludgy, at best. -- NEVIN ":-)" LIBER AT&T Bell Laboratories nevin1@ihlpb.ATT.COM (312) 979-4751