Passing private functions to template algorithms

FreeSlave via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 7 11:45:23 PDT 2016


I want to bring up this unpleasant topic for discussion, because 
I'm really tired of hacks I need to do in my code.

Look at this snippet:

private bool isFileNothrow(string path)
{
     import std.file : isFile;
     import std.exception : collectException;
     bool ok;
     collectException(path.isFile, ok);
     return ok;
}

void main(string[] args)
{
     import std.algorithm : filter;
     auto r = args.filter!(isFileNothrow);
}

Looks good, but it won't compile because isFileNothrow is 
invisible to std.algorithm.

Technically it's right: function is private, so no access from 
other modules. But it makes me to do hacks like

auto r = args.filter!(p => p.isFileNothrow);

I don't see a way how can I reuse my functions while keeping it 
private without ugly hacks.

How about allowing templates from other modules to have access to 
private functions? I think this would be pretty legal behavior, 
since template is instantiated in this module anyway.


More information about the Digitalmars-d mailing list