www.digitalmars.com         C & C++   DMDScript  

D - Templated functions, argument deduction

reply tunah.d tunah.net writes:
A few more questions about templates:

1) Is an abbreviated syntax for templated functions planned, similar to that
with classes? Something like (although this particular syntax might not be
easily parseable)
T max(T)(T x, T y) {
return x > y ? x : y;
}
Which could be called like
int i=max!(int)(0,5);

2) If the above happened, would it be possible to template operator overloading?
Admittedly it would look ugly without...

3) Argument deduction. I think this is important because if templates are uglier
(despite being "better") than the alternative, they won't be used. I'll
certainly prefer 
x > y ? x : y;
to TMax!(typeof(x)).max(x,y),
but max(x,y) is perfect.
And what about:
class Set(T) {
..
Set operPlus(S : T)(Set!(S) s) {
.. // union of this and s
}
}
So you can do
Set!(Object) s;
Set!(Stream) t;
s + t; // has type Set!(Object)
t + s; // has type Set!(Object)
or if templates allowed "super" for a superclass as well as :, 
class Set(T) {
..
Set operPlus(S super T,R : S)(Set!(R) r) {
.. // union of this and r
}
}
And T would become the closest common ancestor of the two operands, allowing
Set!(MemoryStream) s;
Set!(FileStream) t;
s + t; // has type Set!(Stream);
Is there any chance of argument deduction getting into the language?
Sam
Feb 06 2004
parent reply "Ben Hinkle" <bhinkle4 juno.com> writes:
<tunah.d tunah.net> wrote in message news:c01vq5$vi0$1 digitaldaemon.com...
| A few more questions about templates:
|
| 1) Is an abbreviated syntax for templated functions planned, similar to
that
| with classes? Something like (although this particular syntax might not be
| easily parseable)
| T max(T)(T x, T y) {
| return x > y ? x : y;
| }
| Which could be called like
| int i=max!(int)(0,5);

A few months ago this came up but it doesn't seem like a high priority - or
even in planning.

| 2) If the above happened, would it be possible to template operator
overloading?
| Admittedly it would look ugly without...

Already possible with enough aliases:
template Max(T)
{
   T max(T x,T y) { return x > y ? x : y;}
}
alias Max!(int).max max;
alias Max!(double).max max;

int
main()
{
   printf("%d %e\n", max(3,4), max(3.0,-9.9));
   return 0;
}

Note this approach would mean you'd have to make sure both arguments have
the same type.

| 3) Argument deduction. I think this is important because if templates are
uglier
| (despite being "better") than the alternative, they won't be used. I'll
| certainly prefer
| x > y ? x : y;
| to TMax!(typeof(x)).max(x,y),
| but max(x,y) is perfect.
| And what about:
| class Set(T) {
| ..
| Set operPlus(S : T)(Set!(S) s) {
| .. // union of this and s
| }
| }
| So you can do
| Set!(Object) s;
| Set!(Stream) t;
| s + t; // has type Set!(Object)
| t + s; // has type Set!(Object)
| or if templates allowed "super" for a superclass as well as :,
| class Set(T) {
| ..
| Set operPlus(S super T,R : S)(Set!(R) r) {
| .. // union of this and r
| }
| }
| And T would become the closest common ancestor of the two operands,
allowing
| Set!(MemoryStream) s;
| Set!(FileStream) t;
| s + t; // has type Set!(Stream);
| Is there any chance of argument deduction getting into the language?

Again unknown. My guess is it might at some point but not really any time
soon. Then again, maybe Walter has done it and it's just waiting for the
next release. The general flavor of D is that it favors simple name
resolution rules so I wouldn't hold my breath.

| Sam
|
|
Feb 07 2004
parent reply Sam McCall <tunah.d tunah.net> writes:
Ben Hinkle wrote:
 Already possible with enough aliases:
 template Max(T)
 {
    T max(T x,T y) { return x > y ? x : y;}
 }
 alias Max!(int).max max;
 alias Max!(double).max max;
Ah, interesting, didn't cross my mind you can use the same alias name twice. Of course if this was in a library, user defined types have to define the alias themselves. Gives a nice inefficient way to do type deduction: you type alias Func!(,,).func func; and the compiler adds all the aliases for every triple of available types ;-)
 Again unknown. My guess is it might at some point but not really any time
 soon. Then again, maybe Walter has done it and it's just waiting for the
 next release. The general flavor of D is that it favors simple name
 resolution rules so I wouldn't hold my breath.
Could still be simple frolm a user's perspective, 1) Matches exactly 2) Matches exactly with a template 3) Matches with implicit conversions 4) Matches with implicit conversions and a template and ambiguities are errors. But as to how implementable this is, I have no idea. Sam
Feb 07 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Sam McCall wrote:

 soon. Then again, maybe Walter has done it and it's just waiting for the
 next release. The general flavor of D is that it favors simple name
 resolution rules so I wouldn't hold my breath.
Again unknown. My guess is it might at some point but not really any time Could still be simple frolm a user's perspective, 1) Matches exactly 2) Matches exactly with a template 3) Matches with implicit conversions 4) Matches with implicit conversions and a template and ambiguities are errors. But as to how implementable this is, I have no idea.
It would be nice if this were possible: template All(alias name) { alias name!(int).name name; alias name!(float).name name; } template Max(T) { T Max(T x,T y) { return x > y ? x : y;} } alias All!(Max) all; void main() { all.Max(10, 10); } Actually being able to pass method/function names to templates could be helpful in many places (ie kinda-like static delegates). -- -Anderson: http://badmama.com.au/~anderson/
Feb 07 2004
parent "davepermen" <davepermen hotmail.com> writes:
template max(T) {
    T max(T a,T b) { return a > b ? a : b; }
}

max!(int)(x,y);

this works.

but yes, type deduction would be great..

max?(param1,param2) === max!(typeof(param1))(param1,param2)

or simply

template max(alias A,alias B) {
    typeof(A) max(typeof(A) A,typeof(B) B) {
        return A > B ? A : B;
    }
}

max(x,y);

but thats not legal (yet?)



oh, and for the function shortcut..

template(T) T max(T a,T b) { return a > b ? a : b; }

that would be rather simple parsable, i guess..

"J Anderson" <REMOVEanderson badmama.com.au> schrieb im Newsbeitrag
news:c0369o$957$3 digitaldaemon.com...
 Sam McCall wrote:

 soon. Then again, maybe Walter has done it and it's just waiting for
the
 next release. The general flavor of D is that it favors simple name
 resolution rules so I wouldn't hold my breath.
Again unknown. My guess is it might at some point but not really any
time
 Could still be simple frolm a user's perspective,
 1) Matches exactly
 2) Matches exactly with a template
 3) Matches with implicit conversions
 4) Matches with implicit conversions and a template
 and ambiguities are errors. But as to how implementable this is, I
 have no idea.
It would be nice if this were possible: template All(alias name) { alias name!(int).name name; alias name!(float).name name; } template Max(T) { T Max(T x,T y) { return x > y ? x : y;} } alias All!(Max) all; void main() { all.Max(10, 10); } Actually being able to pass method/function names to templates could be helpful in many places (ie kinda-like static delegates). -- -Anderson: http://badmama.com.au/~anderson/
Feb 07 2004