digitalmars.D - Adding finally to switch
- Jesse Phillips (10/10) Mar 31 2008 I haven't given it much thought, but I figured I'd let some other people...
- BCS (15/29) Mar 31 2008 this could be made to work:
- Scott S. McCoy (15/27) Mar 31 2008 My thought on switch has always been: If you're using it as multiple if
- Jesse Phillips (7/9) Apr 01 2008 I would agree, except I don't. :) I thought one of the goals in
- Yigal Chripun (18/30) Apr 01 2008 except, you're forgetting one important issue - multi threading:
- downs (6/18) Mar 31 2008 This can be generalized.
- downs (6/29) Mar 31 2008 I am stupid. If the OP was an april fools, I fell for it hook, line and ...
- Henning Hasemann (12/21) Apr 01 2008 I don't think I get your point. If you have code that is the same in
- Bill Baxter (11/33) Apr 01 2008 Maybe he meant every case except for the default? Still not that big a
- Bill Baxter (4/38) Apr 01 2008 // Doh! Works better if you remember to check the condition
- Jesse Phillips (4/25) Apr 01 2008 Well, I was thinking there might be special constraints added to it, lik...
I haven't given it much thought, but I figured I'd let some other people look at it too. Switch statements are nice, many people hate having to use break; all the time, but I don't and am not interest in the debate. What I think is missing from a switch statement is a finally section. Most of the time I don't have a use for the fall-through feature of switch, but I do have a use for doing one or more things that are the same in every case. As I haven't given it a lot of thought I will leave out some constraint ideas, and just see other peoples thoughts. I don't think it would ruin compatibility of any sort (backwards or C).
Mar 31 2008
Reply to Jesse,I haven't given it much thought, but I figured I'd let some other people look at it too. Switch statements are nice, many people hate having to use break; all the time, but I don't and am not interest in the debate. What I think is missing from a switch statement is a finally section. Most of the time I don't have a use for the fall-through feature of switch, but I do have a use for doing one or more things that are the same in every case. As I haven't given it a lot of thought I will leave out some constraint ideas, and just see other peoples thoughts. I don't think it would ruin compatibility of any sort (backwards or C).this could be made to work: |import std.stdio; | |void main() |{ | switch(1) | { | scope(success) writef("Finaly\n"); | | case 1: writef("first\n"); break; | } |} it doesn't work because it's implemented the same way exceptions are (I think) but doesn't need to be.
Mar 31 2008
My thought on switch has always been: If you're using it as multiple if statements, you're misusing it. I always looked at switch as an opportunity to optimize, little more. I never really thought the idea of switching on strings or similar was really that sweet. If you ask me, such a construct is a fine idea, but we should create a different construct for it (and a dispatch table typically works better for that type of thing). That being said, switch is not a tool you use every day. But that's fine, because when you really *need* a real switch, its so incredibly handy it's unreal, and you become grateful it's there and that it's so fast for what it does, and that it lets you gracefully fall through and create "groups" of behavior where you can't do that with other constructs as easily. Cheers, Scott S. McCoy On Tue, 2008-04-01 at 03:56 +0000, Jesse Phillips wrote:I haven't given it much thought, but I figured I'd let some other people look at it too. Switch statements are nice, many people hate having to use break; all the time, but I don't and am not interest in the debate. What I think is missing from a switch statement is a finally section. Most of the time I don't have a use for the fall-through feature of switch, but I do have a use for doing one or more things that are the same in every case. As I haven't given it a lot of thought I will leave out some constraint ideas, and just see other peoples thoughts. I don't think it would ruin compatibility of any sort (backwards or C).
Mar 31 2008
On Mon, 31 Mar 2008 21:52:17 -0700, Scott S. McCoy wrote:My thought on switch has always been: If you're using it as multiple if statements, you're misusing it.I would agree, except I don't. :) I thought one of the goals in programming was to choose tools that would reduce the number of repeats in code. That is to say if I have several things X could be and want to do something different for all of them, would it be logical to only have to write X once instead of if(X == B) else if (X == C) ...? Anyway maybe BCS has a good choice for it.
Apr 01 2008
Jesse Phillips wrote:On Mon, 31 Mar 2008 21:52:17 -0700, Scott S. McCoy wrote:except, you're forgetting one important issue - multi threading: if you're on a single processor/core/PC/etc, switch would be faster then a series of if statements, as it could be implemented as a table. however, on a multi-core/mutli-cpu the exact opposite is true, and it's more general as in the next snippet: if (x == 1) do_a(); if (x > 0) do_b(); if (x < 2) do_c(); the above could be run in parallel (providing of course, that do_a, do_b and do_c are independent) on different cores and it doesn't have to be deterministic (only one case is run). in new code meant to be run on multi-core PCs the above is much better than a switch. --Yigal PS - writing the above made me thinking: do we need a special parallel_switch syntax for code such as the above where all cases are independent from each other and can run in parallel?My thought on switch has always been: If you're using it as multiple if statements, you're misusing it.I would agree, except I don't. :) I thought one of the goals in programming was to choose tools that would reduce the number of repeats in code. That is to say if I have several things X could be and want to do something different for all of them, would it be logical to only have to write X once instead of if(X == B) else if (X == C) ...? Anyway maybe BCS has a good choice for it.
Apr 01 2008
Jesse Phillips wrote:I haven't given it much thought, but I figured I'd let some other people look at it too. Switch statements are nice, many people hate having to use break; all the time, but I don't and am not interest in the debate. What I think is missing from a switch statement is a finally section. Most of the time I don't have a use for the fall-through feature of switch, but I do have a use for doing one or more things that are the same in every case. As I haven't given it a lot of thought I will leave out some constraint ideas, and just see other peoples thoughts. I don't think it would ruin compatibility of any sort (backwards or C).This can be generalized. I would ask to introduce the keyword "then" to indicate "this is run after the previous construct has successfully completed, executing at least one branch." It could be applicable to [do/]while-loops as well as switches. Finally is an already existing keyword, which could lead to problems with parsing. --downs
Mar 31 2008
downs wrote:Jesse Phillips wrote:I am stupid. If the OP was an april fools, I fell for it hook, line and sinker. I had completely forgotten that switch already covers all cases. Ignore this keyword for switches please, folks. (I still think it's a good idea for loops) --downsI haven't given it much thought, but I figured I'd let some other people look at it too. Switch statements are nice, many people hate having to use break; all the time, but I don't and am not interest in the debate. What I think is missing from a switch statement is a finally section. Most of the time I don't have a use for the fall-through feature of switch, but I do have a use for doing one or more things that are the same in every case. As I haven't given it a lot of thought I will leave out some constraint ideas, and just see other peoples thoughts. I don't think it would ruin compatibility of any sort (backwards or C).This can be generalized. I would ask to introduce the keyword "then" to indicate "this is run after the previous construct has successfully completed, executing at least one branch." It could be applicable to [do/]while-loops as well as switches. Finally is an already existing keyword, which could lead to problems with parsing. --downs
Mar 31 2008
Jesse Phillips <jessekphillips gmail.com> wrote:I haven't given it much thought, but I figured I'd let some other people look at it too. Switch statements are nice, many people hate having to use break; all the time, but I don't and am not interest in the debate. What I think is missing from a switch statement is a finally section. Most of the time I don't have a use for the fall-through feature of switch, but I do have a use for doing one or more things that are the same in every case.I don't think I get your point. If you have code that is the same in *every* case why cant you do: do_this_before_each_case(); switch(foo) { // ... } do_this_after_each_case(); Henning -- GPG Public Key: http://gpg-keyserver.de/pks/lookup?op=get&search=0xDDD6D36D41911851
Apr 01 2008
Henning Hasemann wrote:Jesse Phillips <jessekphillips gmail.com> wrote:Maybe he meant every case except for the default? Still not that big a deal, though. Something like this works: bool passed=false; switch(foo) { ... default: passed = true; } do_this_after_each_non_default_case(); --bbI haven't given it much thought, but I figured I'd let some other people look at it too. Switch statements are nice, many people hate having to use break; all the time, but I don't and am not interest in the debate. What I think is missing from a switch statement is a finally section. Most of the time I don't have a use for the fall-through feature of switch, but I do have a use for doing one or more things that are the same in every case.I don't think I get your point. If you have code that is the same in *every* case why cant you do: do_this_before_each_case(); switch(foo) { // ... } do_this_after_each_case(); Henning
Apr 01 2008
Bill Baxter wrote:Henning Hasemann wrote:// Doh! Works better if you remember to check the condition if(!passed)Jesse Phillips <jessekphillips gmail.com> wrote:Maybe he meant every case except for the default? Still not that big a deal, though. Something like this works: bool passed=false; switch(foo) { ... default: passed = true; }I haven't given it much thought, but I figured I'd let some other people look at it too. Switch statements are nice, many people hate having to use break; all the time, but I don't and am not interest in the debate. What I think is missing from a switch statement is a finally section. Most of the time I don't have a use for the fall-through feature of switch, but I do have a use for doing one or more things that are the same in every case.I don't think I get your point. If you have code that is the same in *every* case why cant you do: do_this_before_each_case(); switch(foo) { // ... } do_this_after_each_case(); Henningdo_this_after_each_non_default_case();-bb
Apr 01 2008
On Tue, 01 Apr 2008 10:20:04 +0200, Henning Hasemann wrote:Jesse Phillips <jessekphillips gmail.com> wrote:Well, I was thinking there might be special constraints added to it, like Bill's example, where default is left out. But as he should I guess in isn't too hard to make your own. To which I say never mind.I haven't given it much thought, but I figured I'd let some other people look at it too. Switch statements are nice, many people hate having to use break; all the time, but I don't and am not interest in the debate. What I think is missing from a switch statement is a finally section. Most of the time I don't have a use for the fall-through feature of switch, but I do have a use for doing one or more things that are the same in every case.I don't think I get your point. If you have code that is the same in *every* case why cant you do: do_this_before_each_case(); switch(foo) { // ... } do_this_after_each_case(); Henning
Apr 01 2008