Full closures for D

David B. Held dheld at codelogicconsulting.com
Tue Nov 6 23:32:31 PST 2007


Nathan Reed wrote:
> David B. Held wrote:
>> However, I think it would be nice to get rid of the delegate type 
>> declaration altogether and git ourselves an implicit delegate 
>> argument, like so:
>>
>>    auto base = "/path/to/dir/";
>>    auto absoluteDirs = map({ return base ~ $0; }, readdir(DIR));
>>
> 
> I think you're asking for too much intelligence form the compiler here. 
>   Remember D is statically typed.  Without giving the parameter type for 
> that delegate, we can't verify that its body is well-typed or that the 
> call to map() is well-typed.  It is logically possible to infer the 
> types in this case, but that goes way beyond the scope of what a D 
> compiler should have to do - this isn't ML; we do type checking, but not 
> full type inference.

In order to analyze this properly, we need to look at my (working) 
definition of map():

T[] map(T)(T delegate(T) f, T[] list)
{
    T[] result;
    foreach (e; list) result ~= f(e);
    return result;
}

Ok, what I propose is that

    typeof({ return base ~ $0; }) == T delegate(T)

which is easy enough to infer, because that is the declared type of f, 
to which the delegate is bound!  No brain surgery there.  How do we know 
what T is?  Well, that's easy enough to infer from list.  And you're 
done.  The mechanics of it isn't that hard.  The question is whether the 
idea of implicit arguments makes people's stomachs turn or not.

Dave



More information about the Digitalmars-d mailing list