Inferring function argument types from other argument types

Ali Çehreli acehreli at yahoo.com
Tue Nov 13 09:47:15 PST 2012


On 11/12/2012 06:55 AM, Joseph Rushton Wakeling wrote:

 > I'm just curious
 > if there is any other way to meaningfully determine the type of one
 > function argument based on the type of another. Hence the playing with
 > different template argument formulations.
 >
 > It just feels really difficult to believe that the generic programming
 > in D doesn't extend to allowing you to infer one argument type based on
 > another.

I am pretty sure that should work according to spec under "Argument 
Deduction":

   http://dlang.org/template.html

"
2. If the type specialization is dependent on a type parameter, the type 
of that parameter is set to be the corresponding part of the type argument.
"

struct Foo(_T1, _T2)
{
     alias _T1 T1;
     T1 t1;
}

void foo(T)(T foo, T.T1 x)    // <-- Note T.T1
{
     return func2(x);
}

void main()
{
     auto f = Foo!(size_t, string)();
     foo(f, 42);
}

Although, there is the issue of compiler's not being sure whether the T1 
in T.T1 is a typename or a member variable. (That's why the 'typename' 
keyword is used in such contexts in C++.)

Still, please create a bug report: :)

   http://d.puremagic.com/issues/

Ali



More information about the Digitalmars-d-learn mailing list