Suggestion: "new delegate" - delegates useable outside of the nesting function scope
Markus Dangl
danglm at in.tum.de
Wed Jun 14 13:20:24 PDT 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);
}
More information about the Digitalmars-d
mailing list