type determination

Jacob Carlborg doob at me.com
Mon Nov 26 23:28:33 PST 2012


On 2012-11-27 06:03, dsmith wrote:
> How can I make something like the following work without "Error: cannot
> append type double to type string[]" ?
>
>
> T[] some_function(T[] var) {
>   T[] var2;
>   double a = 12.34;
>   string b = "hello";
>
>   if(typeof(var).stringof == "double") {
>     var2 ~= a;
>   }
>   if(typeof(var).stringof == "string") {
>     var2 ~= b;
>   }
>
> ...
> }

Use static-if, but you shouldn't use .stringof. Check the actual type 
and not the string of the type:

static if(is(typeof(var) == double)) {
     var2 ~= a;
}

static if(is(typeof(var) == string)) {
     var2 ~= b;
}

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list