switch statement with variable branches
    Stefan Koch via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Thu Jan 19 00:48:17 PST 2017
    
    
  
On Thursday, 19 January 2017 at 01:22:56 UTC, Yuxuan Shui wrote:
> Somehow I can't use ubyte variables behind 'case', but ulong 
> works fine. Why is that?
>
> void main() {
> 	alias TestType = ulong; // won't compile if = ubyte
> 	import std.stdio;
> 	TestType a,b,c;
> 	readf("%s %s %s ", &a, &b, &c);
> 	switch(c){
> 		case a: writeln("a");break;
> 		case b: writeln("b");break;
> 		default: assert(false);
> 	}
> }
It is a bug that this code compiled.
Case Variables can only be used on const values, to prevent 
mutation of them inside the switch itself.
try to make the type a const ubyte.
    
    
More information about the Digitalmars-d-learn
mailing list