Full closures for D

Janice Caron caron800 at googlemail.com
Wed Nov 7 00:21:18 PST 2007


On Nov 7, 2007 6:44 AM, David B. Held <dheld at codelogicconsulting.com> wrote:
>
>     my $base = "/path/to/dir/";
>     my @absoluteDirs = map { $_ = $base . $_ } readdir DIR;
>
> Whatever you think about Perl, you have to admit that this is some
> pretty sweet code.

Looks completely unreadable to me. It just doesn't make sense to my brain.

D is C-like, not perl-like. Please let's keep it that way.



> Let's look at the old imperative way to do this:
>
>     string base = "/path/to/dir/";
>     string[] absoluteDirs;
>     foreach (file; readdir(DIR))
>     {
>         absoluteDirs ~= base ~ file;
>     }

This I understand.


> Now given some suitable definitions (which I demonstrate at the bottom),
> this is what we can do in D, with Walter's new closures:
>
>     auto base = "/path/to/dir/";
>     auto absoluteDirs = map((string file) { return base ~ file; },
> readdir(DIR));
>
> We don't have to repeat absoluteDirs, but we still have to repeat file.

But of course. It's the name of a parameter. You ALWAYS have to repeat
parameter names, unless the name is "this". (Or "outer" :) ). This is
what code clear and readable. In a function parameter list, you give
each paramter a name, and then in the function body, you refer to each
paramter by name. Perfect!


> However, I think it would be nice to get rid of the
> delegate type declaration altogether and git ourselves an implicit
> delegate argument

Generally speaking, I don't. Although that said, D already supports
variadic functions, so one could declare a function as accepting a
tuple, and then refer to the arguments as tuple[n]. I do not believe
this would increase readability, however, in cases where functions are
not actually variadic!


>     auto base = "/path/to/dir/";
>     auto absoluteDirs = map({ return base ~ $0; }, readdir(DIR));

Yuk!

Dollar zero? Yuk! Yuk! Yuk!

Please, no!



More information about the Digitalmars-d mailing list