www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - A little thing about function templates

reply mike <vertex gmx.at> writes:
Hi!

I've got a little theoretical question about function template syntax:

A function is declared like

' void foo(int xy) { ... }

and called like

' foo(3);

a function template is declared like

' void foo(T)(T xy) { ... }

and "called" like

' foo!(int)(3);

Now basically what I'm thinking is that if a "function call" has two set=
s  =

of parenthesis, it should be enough for the compiler to know that it's a=
  =

function template:

' void foo(T)(T x) { ... }
' foo(int)(3);

Maybe there's some ambiguity with functions that return a delegate, I  =

haven't thought about that, since this isn't a feature request, just  =

something that occured to me and I just wanted to hear what the experts =
 =

have to say to it :)
I'm not complaining about the !, just wanted to know if it really is  =

necessary or if it would work with only parentheses.

-Mike

-- =

Erstellt mit Operas revolution=E4rem E-Mail-Modul: http://www.opera.com/=
mail/
Feb 12 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"mike" <vertex gmx.at> wrote in message news:op.tnmz53uonxkcto zimmermoos...
------------------------------------

a function template is declared like

' void foo(T)(T xy) { ... }

and "called" like

' foo!(int)(3);

-------------------------------------

Go ahead and try calling that with "foo(3);" instead.  You will be 
pleasantly surprised.  It's something called IFTI. 
Feb 12 2007
parent reply mike <vertex gmx.at> writes:
Ah-ha! Niiiiiice ... I should read the specs more often, there are real =
 =

treasures hidden in D :)

I'm currently finally learning templates (never understood them in C++).=
 I  =

know this isn't possible, but is something like that maybe in the works =
 =

for D 2.0? I suspect that something like this has already been proposed,=
  =

but since I'm just now starting to grok how Ruby works ... anyway:

' void each(T[] array, void delegate(T) dg)
' {
'     foreach(item; array) dg(item);
' }
'
' [1, 2, 3].each(void delegate(int x) { writefln(x); });

Don't know how the principle is called. I mean that  =

using-the-"."-to-say-that-this-should-be-the-first-argument-thing. Would=
  =

be nice to "inject" fake member functions to classes this way.

If a range type would be added, this could even be:

' [1..3].each(void delegate(int x) { writefln(x); });

And maybe using "->" as a shortcut for that whole thing, so it looks a b=
it  =

like Ruby with this syntax:

' [1..3].each -> (x) { writefln(x); }

The compiler could infer the types involved, if the delegate should retu=
rn  =

something, etc. That would be major. And finally two other things from  =

Ruby I'd like to see in D:

' x, y =3D getCursorLocation();  // a must, really!
' foo() unless (doNotCallFoo); // those statement modifiers seem very  =

logical to me

Ruby syntax still looks ugly to me, and I don't think I'll ever like tha=
t,  =

but I absolutely love those features I mentioned above :)

-Mike

Am 12.02.2007, 15:00 Uhr, schrieb Jarrett Billingsley <kb3ctd2 yahoo.com=
:

 "mike" <vertex gmx.at> wrote in message  =

 news:op.tnmz53uonxkcto zimmermoos...
 ------------------------------------

 a function template is declared like

 ' void foo(T)(T xy) { .. }

 and "called" like

 ' foo!(int)(3);

 -------------------------------------

 Go ahead and try calling that with "foo(3);" instead.  You will be
 pleasantly surprised.  It's something called IFTI.

-- = Erstellt mit Operas revolution=E4rem E-Mail-Modul: http://www.opera.com/= mail/
Feb 12 2007
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"mike" <vertex gmx.at> wrote in message news:op.tnm59miynxkcto zimmermoos...

-------------------------------------
I'm currently finally learning templates (never understood them in C++). I
know this isn't possible, but is something like that maybe in the works
for D 2.0? I suspect that something like this has already been proposed,
but since I'm just now starting to grok how Ruby works ... anyway:

' void each(T[] array, void delegate(T) dg)
' {
'     foreach(item; array) dg(item);
' }
'
' [1, 2, 3].each(void delegate(int x) { writefln(x); });

Don't know how the principle is called. I mean that
using-the-"."-to-say-that-this-should-be-the-first-argument-thing. Would
be nice to "inject" fake member functions to classes this way.
-------------------------------------

Actually, it is:






If a range type would be added, this could even be:

' [1..3].each(void delegate(int x) { writefln(x); });

And maybe using "->" as a shortcut for that whole thing, so it looks a bit
like Ruby with this syntax:

' [1..3].each -> (x) { writefln(x); }

The compiler could infer the types involved, if the delegate should return
something, etc. That would be major. And finally two other things from
Ruby I'd like to see in D:

' x, y = getCursorLocation();  // a must, really!
' foo() unless (doNotCallFoo); // those statement modifiers seem very
logical to me

Ruby syntax still looks ugly to me, and I don't think I'll ever like that,
but I absolutely love those features I mentioned above :)

-Mike

Am 12.02.2007, 15:00 Uhr, schrieb Jarrett Billingsley <kb3ctd2 yahoo.com>:

 "mike" <vertex gmx.at> wrote in message 
 news:op.tnmz53uonxkcto zimmermoos...
 ------------------------------------

 a function template is declared like

 ' void foo(T)(T xy) { .. }

 and "called" like

 ' foo!(int)(3);

 -------------------------------------

 Go ahead and try calling that with "foo(3);" instead.  You will be
 pleasantly surprised.  It's something called IFTI.

-- Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/
Feb 12 2007
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"mike" <vertex gmx.at> wrote in message news:op.tnm59miynxkcto zimmermoos...

Uhh, ignore the other post.

--------------------------------------------------------
I'm currently finally learning templates (never understood them in C++). I
know this isn't possible, but is something like that maybe in the works
for D 2.0? I suspect that something like this has already been proposed,
but since I'm just now starting to grok how Ruby works ... anyway:

' void each(T[] array, void delegate(T) dg)
' {
'     foreach(item; array) dg(item);
' }
'
' [1, 2, 3].each(void delegate(int x) { writefln(x); });

Don't know how the principle is called. I mean that
using-the-"."-to-say-that-this-should-be-the-first-argument-thing. Would
be nice to "inject" fake member functions to classes this way.
--------------------------------------------------------

Actually, this is possible ;)

void each(T)(T[] array, void delegate(T) dg)
{
    foreach(item; array) dg(item);
}

void main()
{
    [1, 2, 3].each(delegate void(int x) { writefln(x); });
}

This is an undocumented feature, being able to call functions which take 
arrays as their first argument as if they were member functions.

--------------------------------------------------------
And maybe using "->" as a shortcut for that whole thing, so it looks a bit
like Ruby with this syntax:

' [1..3].each -> (x) { writefln(x); }

The compiler could infer the types involved, if the delegate should return
something, etc. That would be major.
--------------------------------------------------------

You can already do delegate return type inference:

[1, 2, 3].each((int x) { writefln(x); });

--------------------------------------------------------
And finally two other things from
Ruby I'd like to see in D:

' x, y = getCursorLocation();  // a must, really!
' foo() unless (doNotCallFoo); // those statement modifiers seem very
logical to me

Ruby syntax still looks ugly to me, and I don't think I'll ever like that,
but I absolutely love those features I mentioned above :)
--------------------------------------------------------

Multiple return types would be kind of nice.. they could probably be handled 
as implicit 'out' parameters.  I never really liked the "postfix 
conditional" syntax though.  It seems to me like it'd be easy to miss what's 
going on. 
Feb 12 2007
parent reply mike <vertex gmx.at> writes:
 Actually, this is possible ;)
 This is an undocumented feature, being able to call functions which ta=

 arrays as their first argument as if they were member functions.

Great news! This makes some things possible.
 You can already do delegate return type inference:

 [1, 2, 3].each((int x) { writefln(x); });

Now that's the 3rd feature I wasn't aware of ... and I thought I had som= e = experience with D already :) ' channelStrip.activeDevices((Device e) { e.processAudio(inBuffer, = outBuffer); }); That's quite a nice one-liner. I'm planning to rewrite a lot of code, an= d = this is exactly what I was hoping I could do in D 2.0. Now I find out it= = is possible in D 1.0! :) Thanks a lot! -Mike -- = Erstellt mit Operas revolution=E4rem E-Mail-Modul: http://www.opera.com/= mail/
Feb 12 2007
parent reply "Christian Kamm" <kamm nospam.de> writes:
 ' channelStrip.activeDevices((Device e) { e.processAudio(inBuffer,  
 outBuffer); });

In case you don't already know: depending on what channelStrip and activeDevices are, using the foreach syntax might also be worth looking into: foreach(device; &channelStrip.activeDevices) device.processAudio(inBuffer, outBuffer); You can do this by making activeDevices a function that follows the rules for opApply. Christian
Feb 12 2007
parent reply mike <vertex gmx.at> writes:
Am 12.02.2007, 22:35 Uhr, schrieb Christian Kamm <kamm nospam.de>:

 In case you don't already know: depending on what channelStrip and  =

 activeDevices are, using the foreach syntax might also be worth lookin=

 into:

 foreach(device; &channelStrip.activeDevices)
    device.processAudio(inBuffer, outBuffer);

 You can do this by making activeDevices a function that follows the  =

 rules for opApply.

 Christian

Yeah, currently I've got it like that: ' foreach (device; channelStrip.devices) ' { ' if (device is null) continue; ' // do stuff ' } If I can put the null check and different other checks (isActive, = hasGuiWindow, isStereo, receivesMidi, etc.) in different templates and u= se = the template I need together with the collection I want to access = (inputChannel, sendChannel, etc.) all becomes much easier to work with. = I = think basically that's iterators anyway. I've finally groked what's so = nice about them in Ruby :) On a side note it's really interesting how working in Ruby can just = destroy some of your habits/mental blocks from C/C++ land, in this case = I = started seeing that delegates are not only function pointers but a neat = = way to handle control flow. And that's why I'm currently toying around = with them. Note to self: now it's time to finally learn Scheme! :) -Mike -- = Erstellt mit Operas revolution=E4rem E-Mail-Modul: http://www.opera.com/= mail/
Feb 12 2007
parent "Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail erdani.org> writes:
mike wrote:
 Note to self: now it's time to finally learn Scheme! :)

The sheer fact that such talk has started to air around here is very encouraging! Andrei
Feb 12 2007