June 14, 2006
> Implementing static closures in D would be an interesting proposition, as they would technically be objects, though implicitly.  Then there's the problem with RAII class references.  Consider something like:
> 
> char delegate() createFileReader(char[] fileName)
> {
>     auto File f = new File(fileName, FileMode.In);
> 
>     return new delegate char()
>     {
>         return f.getc();
>     };
> }
> 
> This function would return a function that would be used to read (very simply) a file, one character at a time.  The problem is that the File reference in the enclosing scope could not be deleted when createFileReader returns.  I suppose the way around this would be to disallow using RAII class references inside static closures, but.. 


You might find my "AdvancedDelegate" library useful, i recently posted it to the announce newsgroup. Using its templates for partial application of functions and delegates, you can do something very similiar to the code above:

AdvancedDelegate0!(char) createFileReader(char[] fileName)
{
    auto File f = new File(fileName, FileMode.In);

    auto readerFunc = AdvancedDelegate(
        function char(File f)
        {
            return f.getc();
        }
    );
    return readerFunc(f);
}
June 14, 2006
"Markus Dangl" <danglm@in.tum.de> wrote in message news:e6pr8e$28k3$1@digitaldaemon.com...

> You might find my "AdvancedDelegate" library useful, i recently posted it to the announce newsgroup. Using its templates for partial application of functions and delegates, you can do something very similiar to the code above:

I have been eyeing that ;)


June 14, 2006
Markus Dangl wrote:
> You might find my "AdvancedDelegate" library useful, i recently posted it to the announce newsgroup. Using its templates for partial application of functions and delegates, you can do something very similiar to the code above:
> 
> AdvancedDelegate0!(char) createFileReader(char[] fileName)
> {
>     auto File f = new File(fileName, FileMode.In);
> 
>     auto readerFunc = AdvancedDelegate(
>         function char(File f)
>         {
>             return f.getc();
>         }
>     );
>     return readerFunc(f);
> }

Hmm.. what's stopping you from returning a 'normal' delegate, thru return &readerFunc(f).Eval or &readerFunc(f).opCall ? it would make more sense to functions which accept char delegate() and have no idea what AdvancedDelegate0!(char) is :)



-- 
Tomasz Stachowiak  /+ a.k.a. h3r3tic +/
June 15, 2006
> Hmm.. what's stopping you from returning a 'normal' delegate, thru return &readerFunc(f).Eval or &readerFunc(f).opCall ? it would make more sense to functions which accept char delegate() and have no idea what AdvancedDelegate0!(char) is :)

The Eval() method is there to really do the evaluation of the delegate. You can easily "convert" an AdvancedDelegate to a delegate by referencing its Eval() method:


char delegate() createFileReader(char[] fileName)
{
    File f = new File(fileName, FileMode.In);

    auto readerFunc = AdvancedDelegate(
        function char(File f)
        {
            return f.getc();
        }
    );
    return &(readerFunc(f).Eval);
}

I see that this isn't very intuitive, so i'm going to add a "GetDelegate" method and overload the "opCast" operator - you can then also write:

return readerFunc(f).GetDelegate;

or:

return cast(char delegate()) (readerFunc(f));
1 2
Next ›   Last »