static switch

vitamin vitamin at vitamin.sk
Wed Mar 5 09:00:19 PST 2014


On Wednesday, 5 March 2014 at 12:51:23 UTC, Dominikus Dittes
Scherkl wrote:
> I wonder why there is no static switch (analogue to static if)?
> Because especially if you overload a lot of operators (which is 
> done e.g.
> in the definition of complex and bigint) I would think 
> something like
>
> T opOpAssign(string op, T)(T x) if(op=="+" || op=="-" || 
> op=="*" || op=="/" || op=="%" || op=="^^" || op=="&" || op=="|" 
> || op=="^" || op=="<<" || op==">>" || op==">>>")
> {
>    static switch(op)
>    {
>    case "+":
>    case "-":
>       ...
>       break;
>    case "*":
>       ...
>    }
> }
>
> would look much better than
>
> {
>    static if(op=="+" || op == "-")
>    {
>       ...
>    }
>    else static if(op == "*")
>    {
>       ...
>    }
>    else
>    {
>       ...
>    }
> }

use:

bool In(Args...)(string str, Args args){
	foreach(a; args)
		if(str == a)return true;
	return false;
}

class Foo{

	T opOpAssign(string op, T)(T x) if(op.In("+", "-", "*", "/",
"%", "^^", "&", "|", "^", "<<", ">>", ">>>")){
		return x;
	}
	
}


More information about the Digitalmars-d mailing list