Thread overview | |||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 03, 2008 No more fall through in case statement? | ||||
---|---|---|---|---|
| ||||
http://pragmatic.oreilly.com/pub/a/oreilly/dotnet/news/programmingCsharp_0801.html I think I love the trap no.9 -- 使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/ |
January 03, 2008 Re: No more fall through in case statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to davidl | davidl wrote:
> [...]
> I think I love the trap no.9
Looks like a compromise, it still falls through for empty cases. =)
To me, a warning on nonempty case fallthrough seems more than enough...
regards, frank
|
January 03, 2008 Re: No more fall through in case statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to 0ffh | On 1/3/08, 0ffh <frank@youknow.what.todo.internetz> wrote: > To me, a warning on nonempty case fallthrough seems more than enough... I'm sure you know this already, but: "Warnings are not a defined part of the D Programming Language." (http://digitalmars.com/d/warnings.html) Either it's an error, or it's not. Hey - here's an idea. Why not make case statements have a syntax similar to D's attributes. That is, if you use a colon, execution continutes to end of scope (or break), but if you use curly braces, only the stuff within the braces gets executed. (Just like "public", "private", etc.) For example: case 1: /*...*/ /* falls through */ case 2: /*...*/ break; case 3 { /*...*/ } /* does not fall through */ Existing syntax will still work, so that C++ programmers won't get confused. To get no-fall-through behaviour, you'd have to replace the colon with an opening curly brace (and remember the closing brace). Everyone's happy. (Maybe?) |
January 03, 2008 Re: No more fall through in case statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to davidl | davidl wrote:
> http://pragmatic.oreilly.com/pub/a/oreilly/dotnet/news/programmingCsharp_0801.html
>
>
> I think I love the trap no.9
>
I'm no C# guy but that looks like a nice way to fix C's switch. "goto case 5" looks like something straight out of Walter's playbook, too.
But it's been discussed many times now and it doesn't seem likely Walter's going to change it in D.
--bb
|
January 03, 2008 Re: No more fall through in case statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | Janice Caron Wrote:
> On 1/3/08, 0ffh <frank@youknow.what.todo.internetz> wrote:
> > To me, a warning on nonempty case fallthrough seems more than enough...
>
> I'm sure you know this already, but: "Warnings are not a defined part of the D Programming Language." (http://digitalmars.com/d/warnings.html)
>
> Either it's an error, or it's not.
>
> Hey - here's an idea. Why not make case statements have a syntax similar to D's attributes. That is, if you use a colon, execution continutes to end of scope (or break), but if you use curly braces, only the stuff within the braces gets executed. (Just like "public", "private", etc.)
>
> For example:
>
> case 1:
> /*...*/
> /* falls through */
> case 2:
> /*...*/
> break;
> case 3
> {
> /*...*/
> }
> /* does not fall through */
>
> Existing syntax will still work, so that C++ programmers won't get confused. To get no-fall-through behaviour, you'd have to replace the colon with an opening curly brace (and remember the closing brace). Everyone's happy. (Maybe?)
It's creative, and syntactically coherent. I like it. Even if it breaks my old programs, this one makes sense. : )
|
January 03, 2008 Re: No more fall through in case statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | Janice Caron wrote: > On 1/3/08, 0ffh <frank@youknow.what.todo.internetz> wrote: >> To me, a warning on nonempty case fallthrough seems more than enough... > > I'm sure you know this already, but: "Warnings are not a defined part > of the D Programming Language." > (http://digitalmars.com/d/warnings.html) Right-ho! I do, but I was thinking of C#, what the article was about. =) Actually, I think that "no warnings" is a mistake, as is "no inline". > [...] > > For example: > > case 1: > /*...*/ > /* falls through */ > case 2: > /*...*/ > break; > case 3 > { > /*...*/ > } > /* does not fall through */ > > [...] > Everyone's happy. (Maybe?) I like it as it is. If it absolutely has to be changed, why not replace the explicit break with an implicit break and make fall- through explicit? Liek so: case 1: /*...*/ fallthrough; // falls through case 2: /*...*/ // no fallthrough -> implicit break case ... I could live with that as a compromise... regards, frank |
January 03, 2008 Re: No more fall through in case statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dan | On 1/3/08, Dan <murpsoft@hotmail.com> wrote:
> It's creative, and syntactically coherent. I like it. Even if it breaks my old programs, this one makes sense. : )
I don't think it would break any old programs at all (which means, maybe it has a fighting chance of getting accepted?). All old and existing code would behave exactly as before, without change. That's because the new behaviour would only come into effect if you used the new syntax. Specifically:
case x:
(with a colon) implies the existing C behavior - keep executing code until you reach a break, a continue, or a right-brace. Wheras:
case x { /*...*/ }
(without a colon, but with a left-brace) would mean to execute only that which was within the braces. This construction will never occur in old code, because in existing switch/case syntax, the colon in mandatory.
|
January 03, 2008 Re: No more fall through in case statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | "Janice Caron" wrote
> On 1/3/08, Dan wrote:
>> It's creative, and syntactically coherent. I like it. Even if it breaks my old programs, this one makes sense. : )
>
> I don't think it would break any old programs at all
I haven't done this in any d programs, but in C++ programs, I used to use braces to create a scope in which to declare a variable. What would happen here:
case x:
{
int y = 2;
...
}
break;
I'm not super excited about there being such a subtle difference with the colon (if you automatically put a colon without thinking about it, then the break statement is required, meaning a very subtle bug), but other than that, I like the idea. Why not make it more obvious?:
case (x, y, z) // similar to case x: case y: case z:
{
}
/* else case(a, b, c) ? */
-Steve
|
January 03, 2008 Re: No more fall through in case statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | On Thu, 03 Jan 2008 12:03:08 +0100, Janice Caron <caron800@googlemail.com> wrote: > For example: > > case 1: > /*...*/ > /* falls through */ > case 2: > /*...*/ > break; > case 3 > { > /*...*/ > } > /* does not fall through */ > > Existing syntax will still work, so that C++ programmers won't get > confused. To get no-fall-through behaviour, you'd have to replace the > colon with an opening curly brace (and remember the closing brace). > Everyone's happy. (Maybe?) I've posted this exact same idea a long time ago. It's just so obviously how it should be. -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ |
January 03, 2008 Re: No more fall through in case statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to 0ffh | > I like it as it is. If it absolutely has to be changed, why not replace the explicit break with an implicit break and make fall- through explicit? Liek so: > > case 1: > /*...*/ > fallthrough; // falls through > case 2: > /*...*/ > // no fallthrough -> implicit break > case ... > > I could live with that as a compromise... But the cases are labels. You fall through labels. To change this for switches would be inconsistent. Actually, I don't like switch statement syntax at all, really. I don't think they should be labels. They should be { } blocks. -- Michiel |
Copyright © 1999-2021 by the D Language Foundation