Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
November 27, 2012 type determination | ||||
---|---|---|---|---|
| ||||
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; } ... } |
November 27, 2012 Re: type determination | ||||
---|---|---|---|---|
| ||||
Posted in reply to dsmith | 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:
template valueToAdd(T : double)
{
T valueToAdd = 12.34;
}
template valueToAdd(T : string)
{
T valueToAdd = "hello";
}
T[] some_function(T)(T[] var) {
T[] var2;
var2 ~= valueToAdd!T;
return var2;
}
import std.stdio;
void main()
{
writeln(some_function([ 1.25, 2.75 ]));
writeln(some_function([ "abc", "xyz" ]));
}
Ali
|
November 27, 2012 Re: type determination | ||||
---|---|---|---|---|
| ||||
Posted in reply to dsmith | 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 |
November 27, 2012 Re: type determination | ||||
---|---|---|---|---|
| ||||
Posted in reply to 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.
|
November 27, 2012 Re: type determination | ||||
---|---|---|---|---|
| ||||
Posted in reply to dsmith | 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
|
November 27, 2012 Re: type determination | ||||
---|---|---|---|---|
| ||||
Posted in reply to 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.
|
Copyright © 1999-2021 by the D Language Foundation