How to complex switch?

Michel Fortin michel.fortin at michelf.com
Thu May 12 08:53:08 PDT 2011


On 2011-05-12 07:13:59 -0400, Matthew Ong <ongbp at yahoo.com> said:

> Anyway to include this cool feature of switch with D in the near future?
> 
> switch(str){
>                    // regexp
> case "abc", "def", "as+b?": s1(); break;
> 
> case "za+", "wd?", "aaa": s2(); break;
> 
> default: s3();
> }

The idea is nice. But it breaks one interesting fundamental property of 
the switch statement: in a switch statement, each 'case' is mutually 
exclusive with every other so that the order of the 'case' labels has 
no importance when choosing which one to jump to. Try it: the compiler 
will generate a compile-time error if two cases attempt to match the 
same value. It also means that you can put 'default' in the middle of 
the 'switch' statement if you want and it won't prevent following cases 
from being matched:

	switch (str) {
		case "za+", "wd?", "aaa": s2(); break;
		default: s3(); break;
		case "abc", "def", "as+b?": s1(); break;
	}

How this is this exclusivity supposed to be enforceable with a regular 
expression (or other kinds of pattern matching) remains nebulous.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list