www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - type determination

reply "dsmith" <ds nomail.com> writes:
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
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
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
parent reply "dsmith" <ds nomail.com> writes:
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.
Nov 27 2012
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
parent "dsmith" <ds nomail.com> writes:
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.
Nov 27 2012