A little thing about function templates / Ruby

mike vertex at gmx.at
Mon Feb 12 07:21:12 PST 2007


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 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 at yahoo.com>:

> "mike" <vertex at gmx.at> wrote in message  
> news:op.tnmz53uonxkcto at 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/



More information about the Digitalmars-d mailing list