Thread overview
Bug? (switch/case with strings)
Jan 21, 2004
Clint Olson
Jan 21, 2004
Vathix
Jan 21, 2004
Clint Olson
Re: [Bug] (switch/case with strings)
Jan 22, 2004
Roel Mathys
January 21, 2004
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
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
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
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;
}