Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
February 07, 2017 Pass type directly to a template function? | ||||
---|---|---|---|---|
| ||||
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") |
February 07, 2017 Re: Pass type directly to a template function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Katko | 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. |
February 07, 2017 Re: Pass type directly to a template function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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.
|
February 08, 2017 Re: Pass type directly to a template function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dukc | 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.
|
February 07, 2017 Re: Pass type directly to a template function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Katko | 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 |
Copyright © 1999-2021 by the D Language Foundation