[Proposal] "Name inference" for function templates

Don Clugston dac at nospam.com.au
Fri Jun 16 02:13:43 PDT 2006


Daniel Keep wrote:
> 
> Markus Dangl wrote:
>> Andrei Khropov schrieb:
>>> I think retyping the same name twice for function templates is not a very
>>> natural way, so I propose omitting the template name if there's only a
>>> single
>>> declaration in the template's body and then this declaration's name is
>>> "inferred " for subsequent use, i.e. :
>>> ------------------------------------------------
>>> template sqr(T)
>>> {
>>>     T sqr( T x )
>>>     {
>>>         return x*x;
>>>     }
>>> }
>>> ------------------------------------------------
>> http://www.digitalmars.com/d/template.html
>> If there's only a single declaration, you can even write:
>>
>> T sqr(T) ( T x )
>> {
>>     return x*x;
>> }
>>
>> Which is already in the language and even shorter than your proposal ;)
>> This is the same for class templates.
> 
>> _<  When did THAT get added?  And why didn't I notice?!
> 
> <sob>  All those extra lines of code... all that extra typing... makes
> you wonder how much of D we don't actually know about :P

More than any of us can possibly imagine, I believe. Including Walter.
I just discovered that

int mult(int x)(int y)
{
      return x*y;
}

also works. The first parameter is a compile-time constant, the second 
is a variable.

int q = mult!(5)(z);

IMHO, not very intuitive. But interesting.



More information about the Digitalmars-d mailing list