View previous topic :: View next topic |
Author |
Message |
h3r3tic
Joined: 30 Mar 2004 Posts: 261 Location: Torun, Poland
|
Posted: Wed Mar 31, 2004 4:54 am Post subject: delegate vs function problem |
|
|
hiya, why doesnt this work when the 'delegate' keyword is replaced by 'function' ?
// Temp.d
void main()
{
int delegate() foo;
foo = delegate int() { return 1; };
foo = delegate int() { return 2; };
}
compile log (with 'function'):
OPTLINK (R) for Win32 Release 7.50B1
Error 1: Previous Definition Different : _D4Temp4main0FZi
--- errorlevel 1 |
|
Back to top |
|
|
qbert
Joined: 30 Mar 2004 Posts: 209 Location: Dallas, Texas
|
Posted: Wed Mar 31, 2004 10:44 am Post subject: |
|
|
You have 3 delegates there, which ones are you replacing ? Show both sets of code and both outputs ?
Q |
|
Back to top |
|
|
h3r3tic
Joined: 30 Mar 2004 Posts: 261 Location: Torun, Poland
|
Posted: Wed Mar 31, 2004 11:46 am Post subject: |
|
|
i mean replacing all 'delegate' keywords with 'function'. whats the second output ? doesnt matter, it just compiles w/o an error then. |
|
Back to top |
|
|
jcc7
Joined: 22 Feb 2004 Posts: 657 Location: Muskogee, OK, USA
|
Posted: Wed Mar 31, 2004 2:59 pm Post subject: |
|
|
I don't have any idea what you're trying to do, but this similar code compiles.
Code: | void main()
{
static int function() foo;
static int ret1(){ return 1; }
static int ret2(){ return 2; }
foo = &ret1;
foo = &ret2;
} |
That does seem strange how your example (with 'function') fails in the link stage. It's there's something objectionable about the code, I'd like to see the compiler catch it. |
|
Back to top |
|
|
h3r3tic
Joined: 30 Mar 2004 Posts: 261 Location: Torun, Poland
|
Posted: Wed Mar 31, 2004 11:50 pm Post subject: |
|
|
Quote: | I don't have any idea what you're trying to do |
heh, that was a simplified example...
at this point:
Code: | foo = function int() { return 1; };
foo = function int() { return 2; }; |
it seems like the compiler is trying to use the 'int' as the function's name o_0 and catches a redefinition: 'Error 1: Previous Definition Different : _D4Temp4main0FZi '.
the specification says:
Quote: | int function(char c) fp;
void test()
{
static int foo(char c) { return 6; }
fp = foo;
}
is exactly equivalent to:
int function(char c) fp;
void test()
{
fp = function int(char c) { return 6;};
} |
and it isn't very clear. if i inserted another function to the latter example, like 'fp = function int(char c) { return 7;};' the compiler according to the specs, would create another 'static int foo(char c)' and give me a name conflict. well it does :P
if i change all 'function'`s to 'delegate'`s it works fine.
F1 F1 F1 F1 ;) |
|
Back to top |
|
|
l8night
Joined: 03 May 2004 Posts: 32 Location: UK
|
Posted: Mon May 03, 2004 5:47 pm Post subject: |
|
|
try the latest dmd (0.86) and read on ...
Code: | void main(){
int function() foo;
static int myfunc(){ return 1; }
foo = &myfunc;
} |
works : a static nested function is a function
Code: | void main() {
int function() foo;
int myfunc(){ return 1; }
foo = &myfunc;
} |
fails with the error "cannot implicitly convert int delegate() to int(*)()"
but
Code: | void main() {
int delegate() foo;
int myfunc(){ return 1; }
foo = &myfunc;
} |
works, because a non static nested function is a lexical closure, (you can think of it as a member function of the stack frame!
it has to be a delegate as it has an associated context, for instance try
Code: | void main()
{
int a = 1;
int myfunc(){ return a; }
int delegate() foo = &myfunc;
printf(" a:?d myfunc:?d foo:?d\n", a, myfunc(), foo() );
a = 5;
printf(" a:?d myfunc:?d foo:?d\n", a, myfunc(), foo() );
}
// outputs --
// a:1 myfunc:1 foo:1
// a:5 myfunc:5 foo:5
// |
in languages such as Perl/Lua foo would be a dynamic closure and thus return 1 (the upvalues or cotext would be a snap shot of the stack) this allows a closure to be returned from a function even if it uses local values, in D you must not use a nested function outside its scope. i.e. if passed a delegate that is an inner function do not store it, as it references the stack where it thinks the locals are (and will not be any more).
Hope this helps. |
|
Back to top |
|
|
|