June 16, 2006
Andrei Khropov wrote:
> ------------------------------------------------
> template(T)
> {
> 	T sqr( T x )
> 	{
> 		return x*x;
> 	}
> }
> ------------------------------------------------
[snip]
> 
> This syntax also looks familiar to C++ guys ;-).

If you're going in that direction, you might want to also make the outer braces optional in this case, as they don't really add anything.

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

I think this would look cleaner, as well as even *more* familiar to people used to C++.
June 16, 2006
In article <e6u86a$226k$1@digitaldaemon.com>, Frits van Bommel says...
>
>Andrei Khropov wrote:
>> ------------------------------------------------
>> template(T)
>> {
>> 	T sqr( T x )
>> 	{
>> 		return x*x;
>> 	}
>> }
>> ------------------------------------------------
>[snip]
>> 
>> This syntax also looks familiar to C++ guys ;-).
>
>If you're going in that direction, you might want to also make the outer braces optional in this case, as they don't really add anything.
>
>template(T) T sqr( T x )
>{
>	return x*x;
>}
>
>I think this would look cleaner, as well as even *more* familiar to people used to C++.

I dislike like this as it explicitly introduces the template parameter throught the redundant word "template" (this is one of my dislikes of C++ templates - the massive code bloat they introduce).  The current syntax:

> T sqr(T) ( T x )
> {
>     return x*x;
> }

Is the way Java Generics do it, and there is no problem parsing it, as it also explicitly introduces T, but there may be arguments made that sqr(T)(T x) is harder to read because it depends on context instead of explicit declaration. The context is a bit harder to notice in this case due to using () as template delimiters instead of &lt; &gt;.

Sean


June 16, 2006
Sean Fritz wrote:
> In article <e6u86a$226k$1@digitaldaemon.com>, Frits van Bommel says...
> 
>>Andrei Khropov wrote:
>>
>>>------------------------------------------------
>>>template(T)
>>>{
>>>	T sqr( T x )
>>>	{
>>>		return x*x;
>>>	}
>>>}
>>>------------------------------------------------
>>
>>[snip]
>>
>>>This syntax also looks familiar to C++ guys ;-).
>>
>>If you're going in that direction, you might want to also make the outer braces optional in this case, as they don't really add anything.
>>
>>template(T) T sqr( T x )
>>{
>>	return x*x;
>>}
>>
>>I think this would look cleaner, as well as even *more* familiar to people used to C++.
> 
> 
> I dislike like this as it explicitly introduces the template parameter throught
> the redundant word "template" (this is one of my dislikes of C++ templates - the
> massive code bloat they introduce).  The current syntax:
> 
> 
>>T sqr(T) ( T x )
>>{
>>    return x*x;
>>}
> 
> 
> Is the way Java Generics do it, and there is no problem parsing it, as it also
> explicitly introduces T, but there may be arguments made that sqr(T)(T x) is
> harder to read because it depends on context instead of explicit declaration.
> The context is a bit harder to notice in this case due to using () as template
> delimiters instead of &lt; &gt;.
> 
> Sean
> 
> 

For my own research language I've adopted a mixture of Pascal and D syntax and I have to say I really like it:

// Generic function to add two 'T's and return a 'T':
function(T) addtwo(a, b : T) : T
{
	return a + b;
}

You get the 'function' keyword which is a great way to introduce additional syntaxes like generics support (T); it also looks readable, clear, and concise.  But that is just my opinion.

-- 
James Dunne
June 17, 2006
>> 
>> 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

The "identifier!( T )" syntax is used for instanciating templates. Personnally,
I think it might be a little bit confusing in a declaration. What about making
it look like a parameterized argument list, by just moving the place where stand
the "!" or use an other, more recognizable symbol ?
T sqr ( T )!( T x ) { return x * x ; }
T sqr ( T ):( T x ) ...
T sqr:( T ) ( T x ) ...

Of course, those syntax proposal are suggestion for people who, unlike me, are not yet satisfied with the current syntax.


June 17, 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.

"You found a Secret!"
Hehe..
I didn't think that would work either.

-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
June 18, 2006
Chris Nicholson-Sauls wrote:
> 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!

I wanted to try it out. It does seem to be an attractive solution. The idea is to make it work as simply as class templates.
June 18, 2006
Sean Fritz wrote:
>> T sqr(T) ( T x )
>> {
>>     return x*x;
>> }
> 
> Is the way Java Generics do it,

I didn't think Java supported function templates (only class templates).

> and there is no problem parsing it,

That's true, although it requires arbitrary lookahead, which isn't a significant problem.

> as it also
> explicitly introduces T, but there may be arguments made that sqr(T)(T x) is
> harder to read because it depends on context instead of explicit declaration.
> The context is a bit harder to notice in this case due to using () as template
> delimiters instead of &lt; &gt;.

The < and > cause a lot more ambiguity problems than they solve.
June 18, 2006
In article <e7346j$1uo9$2@digitaldaemon.com>, Walter Bright says...
>
>Sean Fritz wrote:
>>> T sqr(T) ( T x )
>>> {
>>>     return x*x;
>>> }
>> 
>> Is the way Java Generics do it,
>
>I didn't think Java supported function templates (only class templates).

Yes, there are class and method level generics.  There are none of the linking issues that come up from function templates (thank gods!).

Class generics are (of course):

class Foo<T> {  ... }

While method generics are:

public void <T> T[] toArray(T[] arr) { ... }

The distinction in placment of the type parameter is one of the weirdest syntactical choices in Java.  It doesn't take long to master, but it constantly leaves you wondering why they did it.

Also, they aren't really anything like templates except that they allow generic programming.  It really takes several months to fully grok generics (especially with the Java Language Spec being the only decent reference at the moment).

Sean


June 18, 2006
Walter Bright schreef:
> 
> I wanted to try it out. It does seem to be an attractive solution. The idea is to make it work as simply as class templates.

He wanted to try it out, putting in new features without us knowing it. Nice thing anyway Walter.
June 18, 2006
Sean Fritz wrote:
> 
> While method generics are:
> 
> public void <T> T[] toArray(T[] arr) { ... }
> 

Nope, it's either:
  public <T> T[] toArray(T[] arr) { ... }
or:
  public <T> void toArray(T[] arr) { ... }

-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D