Pattern matching in D

Walter Bright newshound1 at digitalmars.com
Sun Mar 7 22:05:19 PST 2010


BCS wrote:
> I think what retard was asking was what types are legal as the argument 
> for a switch?
> 
> IIRC the list is: all the arithmetic types and the string types.
> 
> The value pattern matching that is being asked for would allow just 
> about anything that has a compile time literal syntax:
> 
> void fn(int[] ar)
> {
>   switch(ar)
>   {
>      case [1,2,3]: ... break;
>      case [1,2,4]: ... break;
>      case [1,3,2]: ... break;
>   }
> 
> }

I've thought more than once about adding that, but it just seems 
pointless. I've never run into a use case for it. If you do run into one,

     if (ar == [1,2,3]) ...
     else if (ar == [1,2,4]) ...
     else if (ar == [1,3,2]) ...

will work just fine.



More information about the Digitalmars-d mailing list