type determination

dsmith ds at nomail.com
Tue Nov 27 02:30:31 PST 2012


On Tuesday, 27 November 2012 at 07:28:34 UTC, Jacob Carlborg 
wrote:
> 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;
> }

Oh, the "static if" ... for compile time evaluation; seems 
unintuitive (no?) since data is encountered at run time.



More information about the Digitalmars-d-learn mailing list