Passing private functions to template algorithms

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 7 11:59:03 PDT 2016


On 07.06.2016 20:45, FreeSlave wrote:
> 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.

I think it is obvious that this should work. Visibility checks need to 
happen during identifier lookup. This lookup is happening in the module 
where isFileNothrow is visible.


More information about the Digitalmars-d mailing list