foreach, an analogy

renox renosky at free.fr
Tue Oct 24 14:38:54 PDT 2006


Jarrett Billingsley wrote:

> "Bill Baxter" <dnewsgroup at billbaxter.com> wrote in message 
> news:eh6rva$1anj$2 at digitaldaemon.com...
> 
> 
>>I think it may be Ruby blocks.
> 
> 
> They're just anonymous functions which happen to come after the function 
> call's closing paren.. I wouldn't really say that they're incredibly 
> earth-shattering or the answer to everything.  And D can almost do them 
> already.  Instead of:
> 
> something.each do |item|
>     puts item
> end
> 
> You can have:
> 
> something.each((int item) {
>     writefln(item);
> });
> 
> In fact the "allowing a trailing function literal" has been proposed (by 
> myself included), which would allow:
> 
> something.each()(int item)
> {
>     writefln(item);
> }

You know, personally I prefer the first way to do it than the second, 
it's "more orthogonal/clean".
I wonder why it's not used more? It seems quite readable, maybe there is 
a performance impact (shouldn't happen if the compiler inline the each 
function call).

Of course both ways are a little more verbose than Ruby due to the 
static typing, but they are still very good.

I wonder if with templates there couldn't be a way to have the "item" 
element declared implicitely with the correct type?

Regards,
renoX


> // maybe there'd need to be a semicolon here?
> 
> Which is damn close if you ask me. 
> 
> 



More information about the Digitalmars-d-announce mailing list