FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

delegate vs function problem

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
h3r3tic



Joined: 30 Mar 2004
Posts: 261
Location: Torun, Poland

PostPosted: Wed Mar 31, 2004 4:54 am    Post subject: delegate vs function problem Reply with quote

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
View user's profile Send private message MSN Messenger
qbert



Joined: 30 Mar 2004
Posts: 209
Location: Dallas, Texas

PostPosted: Wed Mar 31, 2004 10:44 am    Post subject: Reply with quote

You have 3 delegates there, which ones are you replacing ? Show both sets of code and both outputs ?

Q
Back to top
View user's profile Send private message MSN Messenger
h3r3tic



Joined: 30 Mar 2004
Posts: 261
Location: Torun, Poland

PostPosted: Wed Mar 31, 2004 11:46 am    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
jcc7



Joined: 22 Feb 2004
Posts: 657
Location: Muskogee, OK, USA

PostPosted: Wed Mar 31, 2004 2:59 pm    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
h3r3tic



Joined: 30 Mar 2004
Posts: 261
Location: Torun, Poland

PostPosted: Wed Mar 31, 2004 11:50 pm    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
l8night



Joined: 03 May 2004
Posts: 32
Location: UK

PostPosted: Mon May 03, 2004 5:47 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group