digitalmars.D.learn - type determination
- dsmith (14/14) Nov 26 2012 How can I make something like the following work without "Error:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (22/36) Nov 26 2012 One way is to dispatch the selection to a "traits" template:
- Jacob Carlborg (11/25) Nov 26 2012 Use static-if, but you shouldn't use .stringof. Check the actual type
- dsmith (4/32) Nov 27 2012 Oh, the "static if" ... for compile time evaluation; seems
- Jonathan M Davis (4/6) Nov 27 2012 But the types are known at compile time. If you're doing anything with t...
- dsmith (3/10) Nov 27 2012 Indeed, based on the parameter in the function call.
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; } ... }
Nov 26 2012
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
Nov 26 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
Nov 26 2012
On Tuesday, 27 November 2012 at 07:28:34 UTC, Jacob Carlborg wrote:On 2012-11-27 06:03, dsmith wrote:Oh, the "static if" ... for compile time evaluation; seems unintuitive (no?) since data is encountered at run time.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; }
Nov 27 2012
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
Nov 27 2012
On Tuesday, 27 November 2012 at 10:45:19 UTC, Jonathan M Davis wrote:On Tuesday, November 27, 2012 11:30:31 dsmith wrote:Indeed, based on the parameter in the function call.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
Nov 27 2012