Alias template parameter to a private function
Sebastien Alaiwan via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jun 24 02:04:18 PDT 2017
Hi,
I'm trying to call std.algorithm.iteration.filter with a private
function as a predicate.
Here's a reduced example code:
// yo.d
import std.algorithm;
void moduleEntryPoint()
{
privateFunction1();
privateFunction2();
}
private:
void privateFunction1()
{
auto array = [0, 1, 2, 3, 4, 5];
auto result = filter!isValid(array); // error: 'isValid' is
private
}
void privateFunction2()
{
auto array = [0, 1, 2, 3, 4, 5];
auto result = filter!isValid(array); // error: 'isValid' is
private
}
bool isValid(int i)
{
return i % 2 == 0;
}
Here's the compiler output:
/usr/include/dmd/phobos/std/algorithm/iteration.d(1132): Error:
function yo.isValid is not accessible from module iteration
yo.d(14): Error: template instance
std.algorithm.iteration.filter!(isValid).filter!(int[]) error
instantiating
This seems like the compiler, when instanciating the calls to
'filter', is resolving 'isValid' from std.algorithm.iteration
scope (however, this isn't actually the case, see below).
I was expecting this identifier to be resolved from yo.d, where
we have access to the private functions.
Surprisingly, the following works:
void privateFunction2()
{
static bool isValid(int i)
{
return i % 2 == 0;
}
auto array = [0, 1, 2, 3, 4, 5];
auto result = filter!isValid(array); // error: 'isValid' is
private
}
This makes the instanciation of 'filter' "see" 'isValid',
however, now, the other privateFunctions can't use it.
Am I missing something here?
Thanks!
More information about the Digitalmars-d-learn
mailing list