www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Pass type directly to a template function?

reply Chris Katko <ckatko gmail.com> writes:
Can I pass a type, instead of a variable of a type, to a template 
function in order to decide the datatype of T in a function?

void function(T)(T x) //works
      {
     T data;
     //do stuff with T, ignoring x.
     }


void function2(T)() //hypothetical, specify the type... somehow?
     {
     T data;
     }

void function3(T)(T) //hypothetical, specify the datatype in the 
argument list
     {
     T data;
     }


void main()
     {
     float f=0;
     float d=0;
     function1(f); //works
     function1(d); //works

     function2!float(); //?
     function3!float(); //?

     function3(float);  //?
     function3(double); //?
     }



It seems like this would be a useful construct for Factory 
pattern that assembles any class that you specify as long as the 
called methods work out. (ala Duck Typing, "if it walks() and 
quacks() like a duck, it's a duck")
Feb 07 2017
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Tuesday, 7 February 2017 at 09:17:04 UTC, Chris Katko wrote:
 Can I pass a type, instead of a variable of a type, to a 
 template function in order to decide the datatype of T in a 
 function?
Yes. That's rather the point.
     function1(f); //works
That is actually shorthand for this: function1!float(f); The compiler is inferring the type of f for you.
     function2!float(); //?
     function3!float(); //?
Yes, this is how it's done.
     function3(float);  //?
     function3(double); //?
No. This won't compile.
 It seems like this would be a useful construct for Factory 
 pattern that assembles any class that you specify as long as 
 the called methods work out. (ala Duck Typing, "if it walks() 
 and quacks() like a duck, it's a duck")
The range infrastructure is based on this concept.
Feb 07 2017
parent reply Dukc <ajieskola gmail.com> writes:
On Tuesday, 7 February 2017 at 10:21:20 UTC, Mike Parker wrote:
     function2!float(); //?
     function3!float(); //?
Yes, this is how it's done.
Not quite with function3, because it takes one unnamed runtime parameter. It can be called like function1 however. The value of the parameter does not matter because it's unused, only the type.
Feb 07 2017
parent Mike Parker <aldacron gmail.com> writes:
On Tuesday, 7 February 2017 at 21:40:04 UTC, Dukc wrote:
 On Tuesday, 7 February 2017 at 10:21:20 UTC, Mike Parker wrote:
     function2!float(); //?
     function3!float(); //?
Yes, this is how it's done.
Not quite with function3, because it takes one unnamed runtime parameter. It can be called like function1 however. The value of the parameter does not matter because it's unused, only the type.
Yes, I missed the runtime parameter.
Feb 07 2017
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 02/07/2017 01:17 AM, Chris Katko wrote:

 void function3(T)(T) //hypothetical, specify the datatype in the
 argument list
     {
     T data;
     }
Related: https://dlang.org/library/object/type_info.html and https://dlang.org/library/object/object.factory.html This compiles but I'm not sure how to use it effectively: import std.stdio; auto function3(TypeInfo ti) { return ti.initializer(); } void main() { writeln(function3(typeid(float))); writeln(function3(typeid(double))); } Ali
Feb 07 2017