Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
January 21, 2004 Bug? (switch/case with strings) | ||||
---|---|---|---|---|
| ||||
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; } |
January 21, 2004 Re: Bug? (switch/case with strings) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Clint Olson | 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.
|
January 21, 2004 Bug? (switch/case with strings) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | 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? |
January 22, 2004 Re: [Bug] (switch/case with strings) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Clint Olson | 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;
}
|
Copyright © 1999-2021 by the D Language Foundation