digitalmars.D.learn - Pass type directly to a template function?
- Chris Katko (31/31) Feb 07 2017 Can I pass a type, instead of a variable of a type, to a template
- Mike Parker (8/20) Feb 07 2017 That is actually shorthand for this:
- Dukc (4/7) Feb 07 2017 Not quite with function3, because it takes one unnamed runtime
- Mike Parker (2/11) Feb 07 2017 Yes, I missed the runtime parameter.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (17/22) Feb 07 2017 Related:
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
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); //worksThat 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
On Tuesday, 7 February 2017 at 10:21:20 UTC, Mike Parker wrote: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.function2!float(); //? function3!float(); //?Yes, this is how it's done.
Feb 07 2017
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:Yes, I missed the runtime parameter.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.function2!float(); //? function3!float(); //?Yes, this is how it's done.
Feb 07 2017
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