D - Why do we have switch at all
- Ant (24/24) Dec 10 2003 I might be wrong, this is what I remember from many years ago.
- J Anderson (4/29) Dec 11 2003 By your title I thought you where going to talk about how polymorphism
I might be wrong, this is what I remember from many years ago. I thought switch was created as a special case of test and branch where the compiler could make some optimizations. The benefit for the programmer was also to see that that special type of program flow control is used, as opposed the if/else if/else if where conditions are more flexible. I also thought this was many years ago and that now compilers can produce efficient code with any dumb thing we throw them (this is not 100%). So, as I see it, the "switch" exists to make programs easy to read. As it was mentioned before D has a strange relation with C. Some times tries to keep compatibility at all cost and some times doesn't. How is throwing a fatal error instead of continuing silently keeping compatibility with a converted C program (or programmer)? Not at all! 100% incompatible. So to conclude "switch" is one of the examples where compatibility with C is *NOT* maintained. Ant PS. I didn't know I was gonna reach that conclusion. It shows how litle thought I give to this problem, I realy don't care, I'm just adding a silent "default:break;", as bad as that is...
Dec 10 2003
Ant wrote:I might be wrong, this is what I remember from many years ago. I thought switch was created as a special case of test and branch where the compiler could make some optimizations. The benefit for the programmer was also to see that that special type of program flow control is used, as opposed the if/else if/else if where conditions are more flexible. I also thought this was many years ago and that now compilers can produce efficient code with any dumb thing we throw them (this is not 100%). So, as I see it, the "switch" exists to make programs easy to read. As it was mentioned before D has a strange relation with C. Some times tries to keep compatibility at all cost and some times doesn't. How is throwing a fatal error instead of continuing silently keeping compatibility with a converted C program (or programmer)? Not at all! 100% incompatible. So to conclude "switch" is one of the examples where compatibility with C is *NOT* maintained. Ant PS. I didn't know I was gonna reach that conclusion. It shows how litle thought I give to this problem, I realy don't care, I'm just adding a silent "default:break;", as bad as that is...By your title I thought you where going to talk about how polymorphism (almost) replaces the switch statement. -Anderson
Dec 11 2003