June 22, 2010 Re: Is there ANY chance we can fix the bitwise operator precedence rules? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Leandro Lucarella | On Mon, Jun 21, 2010 at 6:31 PM, Leandro Lucarella <luca@llucax.com.ar> wrote:
> Bill Baxter, el 21 de junio a las 17:13 me escribiste:
>> On Mon, Jun 21, 2010 at 4:24 PM, Leandro Lucarella <luca@llucax.com.ar> wrote:
>> > goto next case; is a little more verbose but very clear to me :)
>> >
>> > Maybe just next case; is a shorter alternative...
>>
>> That would be great if "next" were a D keyword. But I don't think you're going to get Walter to add a keyword just for this.
>
> Damn! Where did I get next from? I don't know...
I think it's a keyword in Perl maybe?
--bb
| |||
June 22, 2010 Re: Is there ANY chance we can fix the bitwise operator precedence rules? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On 6/21/10, Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote: > Then why are people using switch and next to nobody uses fall through (provably including Walter, who thinks is using fall through "all the time")? Do you have some stats from the phobos and dmd source? I ran a crude text pattern program, and it said about 10% of cases fall through in them combined, but since it is so simple, it is surely inaccurate. (It gave 1% for druntime, and 3% for my own code, then 10% for dmd and phobos both. All numbers are rounded to one significant figure. That 3% is surprising - I know I use fallthrough all the time, but most the cases are two cases right next to each other, one one case falling through to default, which my program didn't count. It only considered a case to fallthrough if there was at least one semicolon between it and the next, and it ignored "default". If it included "case '(': case ')':", like in std.regex, it would be much higher, but if the patch disallows that, they'll be riots in the streets, so it isn't going to happen.) Anywho, if my program is even in the right ballpark, I wouldn't call that "next to nobody". 10% is a pretty big amount. On 6/21/10, Leandro Lucarella <luca@llucax.com.ar> wrote: > Well, while, do..while are also redundant, you can do it with for. And you can do for with goto! Indeed. goto is a kind of comfort construct to me. Even if I never use it, it is reassuring to know it is there if I need it. Anything can be built out of goto! > And you can still use fall-through with the proposed semantics of switch, it's just making it explicit instead of implicit. I tend to write // fallthrough right now anyway to silence [s]the incompetent dweebs[/s] my valued colleagues who complain about fallthrough being there, so assuming the patch doesn't make me riot in the streets, I know it isn't really that big of a change. But still, I resist all change on principle anyway :) | |||
June 22, 2010 Re: Is there ANY chance we can fix the bitwise operator precedence rules? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jeff Nowakowski | Jeff Nowakowski wrote:
> On 06/21/2010 05:11 PM, Jonathan M Davis wrote:
>>
>> Having something like "fallthrough" or "goto next case" would of
>> course be even clearer, but those would require new keywords.
>
> I think "fallthrough" would be a perfect keyword to add here. C programmers will immediately recognize it. Switch/case are already specialized keywords, and fallthrough can complete the set. It's unlikely to conflict with existing source, and this is one case where having a conspicuously long keyword is a good thing.
>
> Finally, goto is ugly and continue is strongly associated with looping.
? It's most commonly used in error handling.
But, fallthrough actually is a goto.
| |||
June 22, 2010 Re: Is there ANY chance we can fix the bitwise operator precedence rules? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adam Ruppe | Adam Ruppe wrote: > On 6/21/10, Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote: >> Then why are people using switch and next to nobody uses fall through >> (provably including Walter, who thinks is using fall through "all the >> time")? > > Do you have some stats from the phobos and dmd source? I ran a crude > text pattern program, and it said about 10% of cases fall through in > them combined, but since it is so simple, it is surely inaccurate. (It > gave 1% for druntime, and 3% for my own code, then 10% for dmd and > phobos both. All numbers are rounded to one significant figure. > > That 3% is surprising - I know I use fallthrough all the time, but > most the cases are two cases right next to each other, one one case > falling through to default, which my program didn't count. It only > considered a case to fallthrough if there was at least one semicolon > between it and the next, and it ignored "default". Did you consider situations where the last thing before the case is actually a 'goto' ? Walter does that a lot. > > If it included "case '(': case ')':", like in std.regex, it would be > much higher, but if the patch disallows that, they'll be riots in the > streets, so it isn't going to happen.) > > Anywho, if my program is even in the right ballpark, I wouldn't call > that "next to nobody". 10% is a pretty big amount. > > > > > On 6/21/10, Leandro Lucarella <luca@llucax.com.ar> wrote: >> Well, while, do..while are also redundant, you can do it with for. And >> you can do for with goto! > > Indeed. goto is a kind of comfort construct to me. Even if I never use > it, it is reassuring to know it is there if I need it. Anything can be > built out of goto! > >> And you can still use fall-through with the proposed semantics of >> switch, it's just making it explicit instead of implicit. > > I tend to write > // fallthrough > right now anyway to silence [s]the incompetent dweebs[/s] my valued > colleagues who complain about fallthrough being there, so assuming the > patch doesn't make me riot in the streets, I know it isn't really that > big of a change. But still, I resist all change on principle anyway :) | |||
June 22, 2010 Re: Is there ANY chance we can fix the bitwise operator precedence rules? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote:
> Hello Jonathan,
>
>> "goto case" does seem a bit silly, but I think that it's clearer and less error prone for anyone who understands "goto case."
>
> Say I have some code with a fall through. If I use the goto case X; version, it allows the cases to be freely reordered. OTOH if I use the other option, it doesn't. I think that the more likely error is for someone to rearrange my code and not notice that a case falls thought (with goto case; this causes a bug) rather than rearrange it and expect it to fall through where it doesn't (with goto case X; this causes a bug). For one thing, if you want it to fall thought, you are already thinking about that issue.
>
It seems to be one of those things that definitely differs depending on how you think. At least we have both "goto case" and "goto case XXX," so if it becomes necessary to have a goto of some kind to fall through, we can pick which works best for how we think. If it becomes prevalent enough, it may or may not lead to a set of established ideas on the safest way to do it. At present, you have fallthrough, so most people probably don't use either form of goto.
- Jonathan M Davis
| |||
June 22, 2010 Re: Is there ANY chance we can fix the bitwise operator precedence rules? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Don | On 6/22/10, Don <nospam@nospam.com> wrote:
> Did you consider situations where the last thing before the case is actually a 'goto' ? Walter does that a lot.
Yeah, me too. I counted them the same as break (and continue, return,
and throw).
Here's my source. I know it has some false negatives and some false positives, which is why I rounded so vigorously.
=======
import std.string;
import std.stdio;
void main(string[] args) {
int totalSwitchCount, totalFallthroughCount, totalCaseCount,
totalCaseFallthroughCount;
foreach(arg; args[1..$]) {
auto f = File(arg);
int fallthroughCount = 0;
int switchCount = 0;
bool caseAlreadyOpen;
bool fallthroughAlready;
int caseCount = 0;
int caseFallthroughCount;
string lastLine;
try
foreach(line; f.byLine) {
// if it says "case .*:", but not goto, we'll call it a switch case
if(line.indexOf("case ") != -1 && line.indexOf("goto") == -1 &&
line.indexOf(":") != -1) {
caseCount++;
totalCaseCount++;
if(caseAlreadyOpen) { // the first one doesn't count
if(
// If this line, or the last line (with a semicolon) breaks, it
isn't fallthrough
lastLine.indexOf("break") == -1 && line.indexOf("break") == -1 &&
// ditto for goto, continue, and return
lastLine.indexOf("goto") == -1 && line.indexOf("goto") == -1 &&
lastLine.indexOf("continue") == -1 && line.indexOf("continue") == -1 &&
lastLine.indexOf("return") == -1 && line.indexOf("return") == -1 &&
// and of course, exceptions
lastLine.indexOf("throw") == -1 && line.indexOf("throw") == -1
) {
totalCaseFallthroughCount++;
caseFallthroughCount++;
if(!fallthroughAlready) {
fallthroughCount++;
totalFallthroughCount++;
}
fallthroughAlready = true;
}
} else
caseAlreadyOpen = true;
}
if(line.indexOf("switch") != -1) {
totalSwitchCount++;
switchCount++;
caseAlreadyOpen = false;
fallthroughAlready = false;
}
if(line.indexOf(";") != -1) // only consider lines with semicolons
as a crude way to find non-empty cases and ignore whitespace
lastLine = line.idup;
}
catch(Exception e) { } // i don't care
writefln("%s: %d/%d (%d/%d cases)", arg, fallthroughCount,
switchCount, caseFallthroughCount, caseCount);
}
writefln("Total switches: %d/%d (%.1f%%)", totalFallthroughCount,
totalSwitchCount, cast(float) totalFallthroughCount / totalSwitchCount
* 100);
writefln(" Total cases: %d/%d (%.1f%%)", totalCaseFallthroughCount,
totalCaseCount, cast(float) totalCaseFallthroughCount / totalCaseCount
* 100);
}
=======
I used the try without {} there too, which was mentioned in another thread. I do this often; it never even occurred to me that it wouldn't be allowed. It isn't for if and for, etc, so why would it be for try? I love it.
| |||
June 22, 2010 Re: Is there ANY chance we can fix the bitwise operator precedence rules? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | On Mon, 21 Jun 2010 20:27:32 -0700, Bill Baxter wrote:
> On Mon, Jun 21, 2010 at 6:31 PM, Leandro Lucarella <luca@llucax.com.ar> wrote:
>> Bill Baxter, el 21 de junio a las 17:13 me escribiste:
>>> On Mon, Jun 21, 2010 at 4:24 PM, Leandro Lucarella <luca@llucax.com.ar> wrote:
>>> > goto next case; is a little more verbose but very clear to me :)
>>> >
>>> > Maybe just next case; is a shorter alternative...
>>>
>>> That would be great if "next" were a D keyword. But I don't think you're going to get Walter to add a keyword just for this.
>>
>> Damn! Where did I get next from? I don't know...
>
> I think it's a keyword in Perl maybe?
>
> --bb
It is a keyword in BASIC, at least. :)
FOR i = 1 TO 100
PRINT i
NEXT
-Lars
| |||
June 22, 2010 Re: Is there ANY chance we can fix the bitwise operator precedence rules? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adam Ruppe | On Mon, 21 Jun 2010 20:40:14 -0400, Adam Ruppe <destructionator@gmail.com> wrote:
> What's the point of a switch without implicit fallthrough?
Maintenance. Using if statements instead of switch, you have to repeat the value to test for each of the cases. If you want to change the value being tested, it's one change. And your workaround using a delegate is not very appealing.
I'll also point out that popular languages have a switch statement and don't allow implicit fallthrough, meaning that 100% of switch statements do not have fallthrough. And switch is used quite often in those languages too, so at least some people think it has use besides allowing implcit fallthrough.
I think mostly it's because the meaning of it is so easy to understand when reading/writing it. When you see a switch, you know what it is and what it isn't. An if statement has many possibilities and must be read more carefully.
-Steve
| |||
June 22, 2010 Re: Is there ANY chance we can fix the bitwise operator precedence rules? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Don | On 06/22/2010 12:06 AM, Don wrote:
>> Finally, goto is ugly and continue is strongly associated with looping.
>
> ? It's most commonly used in error handling.
> But, fallthrough actually is a goto.
Do I really have to argue that goto is mostly deprecated, and has been for decades? Lots of programming constructs are specialized versions of goto. Sure, a well placed goto can still be good in some error handling situations, but it's use should be limited.
Anyways, I find it interesting that somebody in another post said they already put //fallthrough comment in their code, which supports that the most obvious and simplest thing to do is make it a keyword. It would also make for a good suggestion in an error message: "fallthrough required".
| |||
June 22, 2010 Re: Is there ANY chance we can fix the bitwise operator precedence rules? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adam Ruppe | On Tue, 22 Jun 2010 02:40:14 +0200, Adam Ruppe <destructionator@gmail.com> wrote: > What's the point of a switch without implicit fallthrough? If you take > that away, it offers nothing that if/elseif doesn't. (Aside from not > retyping the switch(stuff here), which you can bring into a function > anyway, so whoop-de-doo. And I guess some performance boosts in > rearranging the cases, but can't the optimizer do that in if/else > anyway?) > > int Case(in int value) { return myvar == value; } > > if(Case(10)) { > > } else if (Case(20)) { > > } else { /* default */ } > > You can even use goto and labels to simulate goto case. Well, if you like sacrificing clarity for writing more verbose code, feel free. Switch is valuable for its clarity - '} else if (Case(12)) {' is nowhere near as easily parsable as 'case 12'. -- Simen | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply