Search
type determination
Nov 27, 2012
dsmith
Nov 27, 2012
Ali Çehreli
Nov 27, 2012
Jacob Carlborg
Nov 27, 2012
dsmith
Nov 27, 2012
Jonathan M Davis
Nov 27, 2012
dsmith
```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;
}

...
}

```
```On 11/26/2012 09:03 PM, 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;
> }
>
> ...
> }
>
>

One way is to dispatch the selection to a "traits" template:

{
}

{
}

T[] some_function(T)(T[] var) {
T[] var2;
return var2;
}

import std.stdio;

void main()
{
writeln(some_function([ 1.25, 2.75 ]));
writeln(some_function([ "abc", "xyz" ]));
}

Ali
```
```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
```
```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.

```
```On Tuesday, November 27, 2012 11:30:31 dsmith wrote:
> Oh, the "static if" ... for compile time evaluation; seems unintuitive (no?) since data is encountered at run time.

But the types are known at compile time. If you're doing anything with types, it has to be done at compile time.

- Jonathan M Davis
```
```On Tuesday, 27 November 2012 at 10:45:19 UTC, Jonathan M Davis wrote:
> On Tuesday, November 27, 2012 11:30:31 dsmith wrote:
>> Oh, the "static if" ... for compile time evaluation; seems
>> unintuitive (no?) since data is encountered at run time.
>
> But the types are known at compile time. If you're doing anything with types,
> it has to be done at compile time.
>
> - Jonathan M Davis

Indeed, based on the parameter in the function call.

```