Type literal of pure function pointer
bearophile
bearophileHUGS at lycos.com
Sat Jul 24 16:10:54 PDT 2010
In the following D2 the D type system is strong enough to allow foo1() to be pure because sqr() is a pointer to a pure function. In foo2() I have tried to do the same thing avoiding templates, and it works. In foo3() I have tried to write the type literal, but I was not able to:
pure int sqr(int x) {
return x * x;
}
pure int foo1(TF)(TF func, int x) { // OK
return func(x);
}
pure int foo2(typeof(&sqr) func, int x) { // OK
return func(x);
}
pure int foo3(pure int function(int) func, int x) { // line 10, ERR
return func(x);
}
void main() {
assert(foo1(&sqr, 5) == 25);
assert(foo2(&sqr, 5) == 25);
assert(foo3(&sqr, 5) == 25);
}
Errors given, dmd 2.047:
test.d(10): basic type expected, not pure
test.d(10): found 'pure' when expecting ')'
test.d(10): semicolon expected following function declaration
test.d(10): no identifier for declarator int function(int)
test.d(10): semicolon expected, not 'int'
test.d(10): semicolon expected, not ')'
test.d(10): Declaration expected, not ')'
test.d(12): unrecognized declaration
(If you can't find a way to write that then I'll add it to Bugzilla.)
Bye and thank you,
bearophile
More information about the Digitalmars-d-learn
mailing list