www.digitalmars.com         C & C++   DMDScript  

D - Bug? (switch/case with strings)

reply Clint Olson <Clint_member pathlink.com> writes:
I would expect the code at bottom to compile, but it doesn't (Win32 DMD 0.78).
When I remove the case statment using a char[] variable (stringB) it compiles.
It seems only string literals work in case statments.  Is this a bug?

Command line
---------------
C:\sandbox\d\dui>dmd test1.d -oftest1.exe 
test1.d(5): case 'stringB' is not a string

Code
---------------
int main(char[][] args) {
char[] stringA = "A String";
char[] stringB = "A String";

switch (stringA) {    // <-- compiler error: case 'stringB' is not a string
case stringB:
printf("matched stringB");
break;
case "A String":
printf("matched literal string");
break;
default:
printf("fell through to default");
break;
}

return 0;
}
Jan 21 2004
parent reply Vathix <vathix dprogramming.com> writes:
Clint Olson wrote:
 I would expect the code at bottom to compile, but it doesn't (Win32 DMD 0.78).
 When I remove the case statment using a char[] variable (stringB) it compiles.
 It seems only string literals work in case statments.  Is this a bug?
 
 Command line
 ---------------
 C:\sandbox\d\dui>dmd test1.d -oftest1.exe 
 test1.d(5): case 'stringB' is not a string
 
 Code
 ---------------
 int main(char[][] args) {
 char[] stringA = "A String";
 char[] stringB = "A String";
 
 switch (stringA) {    // <-- compiler error: case 'stringB' is not a string
 case stringB:
 printf("matched stringB");
 break;
 case "A String":
 printf("matched literal string");
 break;
 default:
 printf("fell through to default");
 break;
 }
 
 return 0;
 }
Cases of a switch must be constant, as in string literals.
Jan 21 2004
parent reply Clint Olson <Clint_member pathlink.com> writes:
In article <bumtc1$25dn$1 digitaldaemon.com>, Vathix says...
Clint Olson wrote:
 I would expect the code at bottom to compile, but it doesn't (Win32 DMD 0.78).
 When I remove the case statment using a char[] variable (stringB) it compiles.
 It seems only string literals work in case statments.  Is this a bug?
 
 Command line
 ---------------
 C:\sandbox\d\dui>dmd test1.d -oftest1.exe 
 test1.d(5): case 'stringB' is not a string
 
 Code
 ---------------
 int main(char[][] args) {
 char[] stringA = "A String";
 char[] stringB = "A String";
 
 switch (stringA) {    // <-- compiler error: case 'stringB' is not a string
 case stringB:
 printf("matched stringB");
 break;
 case "A String":
 printf("matched literal string");
 break;
 default:
 printf("fell through to default");
 break;
 }
 
 return 0;
 }
Cases of a switch must be constant, as in string literals.
Sure enough, adding "const" to the declaration of "stringB" fixed the code. Interestingly, the second case clause was matched rather than the first. Eliminating the second one allowed the first one to match. Is that a bug?
Jan 21 2004
parent Roel Mathys <roel.mathys yucom.be> writes:
Clint Olson wrote:

 In article <bumtc1$25dn$1 digitaldaemon.com>, Vathix says...
 
Clint Olson wrote:

I would expect the code at bottom to compile, but it doesn't (Win32 DMD 0.78).
When I remove the case statment using a char[] variable (stringB) it compiles.
It seems only string literals work in case statments.  Is this a bug?

Command line
---------------
C:\sandbox\d\dui>dmd test1.d -oftest1.exe 
test1.d(5): case 'stringB' is not a string

Code
---------------
int main(char[][] args) {
char[] stringA = "A String";
char[] stringB = "A String";

switch (stringA) {    // <-- compiler error: case 'stringB' is not a string
case stringB:
printf("matched stringB");
break;
case "A String":
printf("matched literal string");
break;
default:
printf("fell through to default");
break;
}

return 0;
}
Cases of a switch must be constant, as in string literals.
Sure enough, adding "const" to the declaration of "stringB" fixed the code. Interestingly, the second case clause was matched rather than the first. Eliminating the second one allowed the first one to match. Is that a bug?
well both stringA and stringB refer to the string "A String", and I think the switch-statement tests for egality not for identity, so removing the second should give this result. But your original case had twice the same case, which should be illegal, try this: int main() { int a = 0; switch (a) { // <-- compiler error: case 'stringB' is not a string case 0: printf("first zero matched\n"); break; case 0: printf("second zero matched\n"); break; default: printf("fell through to default"); break; } return 0; }
Jan 21 2004