digitalmars.D.learn - Type literal of pure function pointer
- bearophile <bearophileHUGS lycos.com> Jul 24 2010
- "Simen kjaeraas" <simen.kjaras gmail.com> Jul 25 2010
- bearophile <bearophileHUGS lycos.com> Jul 25 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
Jul 24 2010
On Sun, 25 Jul 2010 01:10:54 +0200, bearophile <bearophileHUGS lycos.com> wrote: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
Add it to Bugzilla. Another case is that this works: alias pure int function( int ) FN; pure foo4( FN fn, int x ) { return fn( x ); } It seems the problem is that type specification in function signatures does not support the full range of type signature in the language. -- Simen
Jul 25 2010
Simen kjaeraas:Add it to Bugzilla. Another case is that this works: alias pure int function( int ) FN; pure foo4( FN fn, int x ) { return fn( x ); } It seems the problem is that type specification in function signatures does not support the full range of type signature in the language.
http://d.puremagic.com/issues/show_bug.cgi?id=4505
Jul 25 2010








bearophile <bearophileHUGS lycos.com>