Inferring function argument types from other argument types

Vijay Nayar madric at gmail.com
Mon Nov 12 07:38:35 PST 2012


I think this is what you want.  This example compiled and ran 
using DMD v2.060.

FooType.T1 func(FooType, FooT1)(FooType foo, FooT1 x)
   if (is(FooType.T1) && is(FooT1 : FooType.T1))
{
	return x * foo.a;
}

void main()
{
	auto foo = new Foo!(size_t, real)(3, 2.5);

	writeln(foo.func(4));
}

  - Vijay

On Monday, 12 November 2012 at 13:13:56 UTC, Joseph Rushton 
Wakeling wrote:
> On 11/12/2012 02:02 PM, bearophile wrote:
>> Like this?
>
> Thanks for the thought, but not really :-(  Your code _checks_ 
> that the second argument has the correct type, but it doesn't 
> perform an implicit conversion where that is possible.
>
> In a pseudo-code-y sense, what I want to be able to do is 
> something like:
>
>     auto func(FooT)(FooT foo, FooT.T1 x)
>     {
>         ...
>     }
>
> and indeed I tried that on the off-chance that it'd work; but 
> it fails to compile with an error message "Undefined identifier 
> FooT.T1".
>
> All of the messing around with a second template argument was 
> just in order to be able to achieve something equivalent to the 
> above.




More information about the Digitalmars-d-learn mailing list