Jump to page: 1 2 3
Thread overview
[Proposal] "Name inference" for function templates
Jun 15, 2006
Andrei Khropov
Jun 16, 2006
Markus Dangl
Jun 16, 2006
Sean Kelly
Jun 16, 2006
Markus Dangl
Jun 16, 2006
Andrei Khropov
Jun 16, 2006
Derek Parnell
Jun 16, 2006
Daniel Keep
Jun 16, 2006
Don Clugston
Jun 18, 2006
Walter Bright
Jun 18, 2006
Sjoerd van Leent
Jun 16, 2006
Andrei Khropov
Re: [Proposal]
Jun 17, 2006
Rémy Mouëza
Jun 17, 2006
Bruno Medeiros
Jun 16, 2006
Frits van Bommel
Re: [Proposal]
Jun 16, 2006
Sean Fritz
Jun 16, 2006
James Dunne
Jun 18, 2006
Walter Bright
Jun 18, 2006
Sean Fritz
Jun 18, 2006
Bruno Medeiros
Jun 18, 2006
Dave
Jun 18, 2006
Sjoerd van Leent
Jun 18, 2006
Dave
Jun 19, 2006
Daniel Keep
Jun 19, 2006
xs0
Jun 19, 2006
Mike Parker
Jun 20, 2006
Sean Fritz
Jun 20, 2006
Sean Fritz
June 15, 2006
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;
	}
}
------------------------------------------------

could be replaced by

------------------------------------------------
template(T)
{
	T sqr( T x )
	{
		return x*x;
	}
}
------------------------------------------------

The usage is as usual:

------------------------------------------
int main( char[][] args )
{
    writefln("sqr(5) returns %d", sqr(5) );
    return 0;
}
------------------------------------------

This syntax also looks familiar to C++ guys ;-).

The only obstacle I see is that it may somewhat complicate the compiler.

-- 
AKhropov
June 16, 2006
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.
June 16, 2006
Markus Dangl wrote:
> 
> 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.

Huh.  I don't see any mention of that syntax being legal in the docs, but it works.  And to think I wanted to propose this a while back--I should have just tried it out. :-)


Sean
June 16, 2006
On Fri, 16 Jun 2006 04:35:57 +0200, Markus Dangl wrote:

> 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;
> }
> 

> This is the same for class templates.

Neat! But it is not documented.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
16/06/2006 1:34:48 PM
June 16, 2006

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

	-- Daniel

-- 
Unlike Knuth, I have neither proven or tried the above; it may not even make sense.

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/
June 16, 2006
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.

I know for a definite fact this /did not work/ previously... must be something that happened fairly recently.  A silent enhancement, perhaps just for testing purposes for now?  If I can get a clear from Walter that this is a permanent feature, I am *so* taking advantage of it!

-- Chris Nicholson-Sauls
June 16, 2006
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.

Well, besides that it isn't documented (as far as I can see)
I think it's rather confusing
(two sequential pairs of parameters in parentheses - I a more complicated case
you really don't tell whether it is template or not at the first glance).
It's different from the class case where parameter list in parentheses in
umabiguous.

I've thought of that variant too but it seems to me that it's less readable.

Another variant maybe is to insert "!" before template parameters list in a declaration too:

----------------------------
T sqr!(T) ( T x )
{
     return x*x;
}
----------------------------

-- 
AKhropov
June 16, 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.
June 16, 2006
> Huh.  I don't see any mention of that syntax being legal in the docs, but it works.  And to think I wanted to propose this a while back--I should have just tried it out. :-)

Whoops, i read the wrong part of the doc and used it accidently for function templates - now that you mentioned it i read through the docs again and it's not there ;)
June 16, 2006
Markus Dangl wrote:

> > Huh.  I don't see any mention of that syntax being legal in the docs,  but it works.  And to think I wanted to propose this a while back--I  should have just tried it out. :-)
> 
> Whoops, i read the wrong part of the doc and used it accidently for function templates - now that you mentioned it i read through the docs again and it's not there ;)

D Easter eggs? <g>

-- 
AKhropov
« First   ‹ Prev
1 2 3