www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - should this work?

reply BCS <nothing pathlink.com> writes:
void main()
{
   char[] str = "hello";
   int i = 5;
   switch(str)
   {
     case "hello":
     switch(i)
     {
       case "goodby":	// case for outer switch in inner switch
         writef("foo\n");
       case 1:;
         writef("bar\n");
     }
   }
}

It doesn't because (I assume), it isn't allowed to in C/C++. However 
that is because C has only one type for switches, integer. D doesn't 
have this restriction, array types are just fine. So why not (sanity 
aside*) permit it?

* I can think of no uses for it, but what does that matter.
Dec 29 2006
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"BCS" <nothing pathlink.com> wrote in message 
news:en4131$29h2$1 digitaldaemon.com...
 It doesn't because (I assume), it isn't allowed to in C/C++. However that 
 is because C has only one type for switches, integer. D doesn't have this 
 restriction, array types are just fine. So why not (sanity aside*) permit 
 it?

Ugh, thankfully it doesn't. That's just plain confusing.
Dec 29 2006
prev sibling parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
BCS wrote:

 void main()
 {
    char[] str = "hello";
    int i = 5;
    switch(str)
    {
      case "hello":
      switch(i)
      {
        case "goodby": // case for outer switch in inner switch
          writef("foo\n");
        case 1:;
          writef("bar\n");
      }
    }
 }
 
 It doesn't because (I assume), it isn't allowed to in C/C++. However
 that is because C has only one type for switches, integer. D doesn't
 have this restriction, array types are just fine. So why not (sanity
 aside*) permit it?
 
 * I can think of no uses for it, but what does that matter.

I don't think this should be allowed. To actually enter the "goodby" case, str will have to be changed after case "hello" has been entered. -- Lars Ivar Igesund blog at http://larsivi.net DSource & #D: larsivi
Dec 29 2006
parent BCS <nothing pathlink.com> writes:
Lars Ivar Igesund wrote:
 BCS wrote:
 
 void main()
 {
    char[] str = "hello";
    int i = 5;
    switch(str)
    {
      case "hello":
      switch(i)
      {
        case "goodby": // case for outer switch in inner switch
          writef("foo\n");
        case 1:;
          writef("bar\n");
      }
    }
 }

 It doesn't because (I assume), it isn't allowed to in C/C++. However
 that is because C has only one type for switches, integer. D doesn't
 have this restriction, array types are just fine. So why not (sanity
 aside*) permit it?

 * I can think of no uses for it, but what does that matter.

I don't think this should be allowed. To actually enter the "goodby" case, str will have to be changed after case "hello" has been entered.

Well, assume that str could be "goodby". Say it (and i) comes from somewhere else. the result would be: str i output ----------------------- hello 1 "bar\n" hello !1 error: no default goodby any "foo\nbar\n" It's all moot because it doesn't work and, IMHO, goes along with the duff's device into the category of "things that you probably shouldn't do". :b
Dec 29 2006