Jump to page: 1 2
Thread overview
Adding finally to switch
Apr 01, 2008
Jesse Phillips
Apr 01, 2008
BCS
Apr 01, 2008
Scott S. McCoy
Apr 01, 2008
Jesse Phillips
Apr 01, 2008
Yigal Chripun
Let's call it `then`
Apr 01, 2008
downs
Re: Let's call it `then` - STUPID
Apr 01, 2008
downs
Apr 01, 2008
Henning Hasemann
Apr 01, 2008
Bill Baxter
Apr 01, 2008
Bill Baxter
Apr 01, 2008
Jesse Phillips
April 01, 2008
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).
April 01, 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.


April 01, 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).

April 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
April 01, 2008
downs wrote:
> 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

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)

 --downs
April 01, 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

April 01, 2008
Henning Hasemann wrote:
> 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
> 

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();

--bb
April 01, 2008
Bill Baxter wrote:
> Henning Hasemann wrote:
>> 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
>>
> 
> 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;
> }

// Doh! Works better if you remember to check the condition
if(!passed)
> do_this_after_each_non_default_case();


-bb
April 01, 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.
April 01, 2008
On Tue, 01 Apr 2008 10:20:04 +0200, Henning Hasemann wrote:

> 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

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.
« First   ‹ Prev
1 2