digitalmars.D.learn - switch ( Expression ) ScopeStatement
- Derek Parnell (11/11) Jul 06 2009 I see that the syntax for 'switch' is ...
- Jarrett Billingsley (4/10) Jul 06 2009 So you can write this, of course!
- bearophile (14/15) Jul 06 2009 To allow train-wrecks like this one:
- Derek Parnell (14/29) Jul 06 2009 No quite the same thing, actually. You have highlighted another quirk wi...
- Tim Matthews (14/44) Jul 06 2009 Switch is really a neat form of comparison and gotos but with actual
- Derek Parnell (20/37) Jul 06 2009 Actually that is not valid code. The first "break;" breaks the program.
- Tim Matthews (7/33) Jul 06 2009 yeah I forgot to remove the break as it was no longer part of the switch...
- Tim Matthews (3/25) Jul 06 2009 It shouldn't have accepted it so it's here now.
- Tim Matthews (4/22) Jul 06 2009 You are looking at something completely different but is not limited to
- BCS (6/24) Jul 06 2009 you want a train wreck?
I see that the syntax for 'switch' is ... switch ( Expression ) ScopeStatement and ScopeStatement is either a BlockStatement or a NonEmptyStatement. Meaning that switch (i) j = k; is valid syntax! Why is that? -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Jul 06 2009
On Mon, Jul 6, 2009 at 8:18 PM, Derek Parnell<derek psych.ward> wrote:I see that the syntax for 'switch' is ... =A0 switch ( Expression ) ScopeStatement and ScopeStatement is either a BlockStatement or a NonEmptyStatement. Meaning that =A0switch (i) j =3D k; is valid syntax! Why is that?So you can write this, of course! switch(x) case 1: break; I mean, you sometimes need a one-case switch, yes? ;)
Jul 06 2009
Derek Parnell:is valid syntax! Why is that?To allow train-wrecks like this one: version(Tango) import tango.stdc.stdio; void main(char[][] args) { if (args.length > 1) switch (args[1]) { int x = 1; // This initialization to 1 doesn't happen, // it's the same as int x = void; case "1": printf("1! x=%d\n", x); break; case "2": printf("2! x=%d\n", x); break; } } Bye, bearophile
Jul 06 2009
On Mon, 06 Jul 2009 22:10:20 -0400, bearophile wrote:Derek Parnell:No quite the same thing, actually. You have highlighted another quirk with switch though. My question was really about why is it that the first token after the "( Expression )" did not have to be a left-brace, but could be any statement at all. switch(x) funcA(); switch(x) switch (y) {}; Just seems wrong somehow but I'm positive that the experts have a perfectly rational answer. I just hope I can understand it. -- Derek Parnell Melbourne, Australia skype: derek.j.parnellis valid syntax! Why is that?To allow train-wrecks like this one: version(Tango) import tango.stdc.stdio; void main(char[][] args) { if (args.length > 1) switch (args[1]) { int x = 1; // This initialization to 1 doesn't happen, // it's the same as int x = void; case "1": printf("1! x=%d\n", x); break; case "2": printf("2! x=%d\n", x); break; } }
Jul 06 2009
Derek Parnell wrote:On Mon, 06 Jul 2009 22:10:20 -0400, bearophile wrote:Switch is really a neat form of comparison and gotos but with actual labels replaced with a case statement. A block statement is usually used to have more than one case statement valid. This is valid code but uncomment the next line and it becomes invalid: module test; import tango.io.Stdout; void main(char[][] args) { if (args.length > 1) switch (args[1]) case "1": Stdout("1"); break; //case "2": Stdout("2"); break; //uncomment and error }Derek Parnell:No quite the same thing, actually. You have highlighted another quirk with switch though. My question was really about why is it that the first token after the "( Expression )" did not have to be a left-brace, but could be any statement at all. switch(x) funcA(); switch(x) switch (y) {}; Just seems wrong somehow but I'm positive that the experts have a perfectly rational answer. I just hope I can understand it.is valid syntax! Why is that?To allow train-wrecks like this one: version(Tango) import tango.stdc.stdio; void main(char[][] args) { if (args.length > 1) switch (args[1]) { int x = 1; // This initialization to 1 doesn't happen, // it's the same as int x = void; case "1": printf("1! x=%d\n", x); break; case "2": printf("2! x=%d\n", x); break; } }
Jul 06 2009
On Tue, 07 Jul 2009 14:57:39 +1200, Tim Matthews wrote:Switch is really a neat form of comparison and gotos but with actual labels replaced with a case statement. A block statement is usually used to have more than one case statement valid. This is valid code but uncomment the next line and it becomes invalid: module test; import tango.io.Stdout; void main(char[][] args) { if (args.length > 1) switch (args[1]) case "1": Stdout("1"); break; //case "2": Stdout("2"); break; //uncomment and error }Actually that is not valid code. The first "break;" breaks the program. This is valid ... module test; import tango.io.Stdout; void main(char[][] args) { if (args.length > 1) switch (args[1]) case "1": Stdout("1"); // break; //uncomment and error } Ok, why on earth would a one-case switch be useful? And that still doesn't help me understand why allowing other statement types in that position is a good idea. -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Jul 06 2009
Derek Parnell wrote:On Tue, 07 Jul 2009 14:57:39 +1200, Tim Matthews wrote:yeah I forgot to remove the break as it was no longer part of the switch statement.Switch is really a neat form of comparison and gotos but with actual labels replaced with a case statement. A block statement is usually used to have more than one case statement valid. This is valid code but uncomment the next line and it becomes invalid: module test; import tango.io.Stdout; void main(char[][] args) { if (args.length > 1) switch (args[1]) case "1": Stdout("1"); break; //case "2": Stdout("2"); break; //uncomment and error }Actually that is not valid code. The first "break;" breaks the program.Ok, why on earth would a one-case switch be useful? And that still doesn't help me understand why allowing other statement types in that position is a good idea.C compatibility. It's not really a problem to D. If there is a compiler that only accepted block statements for switch not many people would care that it doesn't conform to C spec because no one uses it like that anyway.
Jul 06 2009
Derek Parnell wrote:On Tue, 07 Jul 2009 14:57:39 +1200, Tim Matthews wrote:It shouldn't have accepted it so it's here now. http://d.puremagic.com/issues/show_bug.cgi?id=3144Switch is really a neat form of comparison and gotos but with actual labels replaced with a case statement. A block statement is usually used to have more than one case statement valid. This is valid code but uncomment the next line and it becomes invalid: module test; import tango.io.Stdout; void main(char[][] args) { if (args.length > 1) switch (args[1]) case "1": Stdout("1"); break; //case "2": Stdout("2"); break; //uncomment and error }Actually that is not valid code. The first "break;" breaks the program.
Jul 06 2009
bearophile wrote:Derek Parnell:You are looking at something completely different but is not limited to switches. Switchs are a bit like gotos and gotos can achieve this same skip initialization errors.is valid syntax! Why is that?To allow train-wrecks like this one: version(Tango) import tango.stdc.stdio; void main(char[][] args) { if (args.length > 1) switch (args[1]) { int x = 1; // This initialization to 1 doesn't happen, // it's the same as int x = void; case "1": printf("1! x=%d\n", x); break; case "2": printf("2! x=%d\n", x); break; } } Bye, bearophile
Jul 06 2009
Hello bearophile,Derek Parnell:you want a train wreck? void main() { switch(5) default: { case 6: {} } }is valid syntax! Why is that?To allow train-wrecks like this one: version(Tango) import tango.stdc.stdio; void main(char[][] args) { if (args.length > 1) switch (args[1]) { int x = 1; // This initialization to 1 doesn't happen, // it's the same as int x = void; case "1": printf("1! x=%d\n", x); break; case "2": printf("2! x=%d\n", x); break; } } Bye, bearophile
Jul 06 2009