digitalmars.D.learn - returning delegates to nested function
- BCS (9/9) Jan 09 2006 Is this valid
- John Reimer (10/20) Jan 09 2006 I'm not so certain that nested functions are truly local (as in local
- BCS (11/36) Jan 09 2006 Yes, the function works fine with DMD. However I'm not shure that it is ...
- Chris Sauls (6/17) Jan 09 2006 I'm not sure what the best way is, but this would probably work:
- BCS (14/36) Jan 09 2006 Thought of that (actually I was think a class, but struct would do bette...
- Ben Hinkle (9/17) Jan 09 2006 If it doesn't use the context pointer it should be fine. I suppose the
- Sean Kelly (7/17) Jan 09 2006 Technically, no. Why not use a function pointer instead?
- John Reimer (3/22) Jan 09 2006 I totally missed the delegate/function pointer context here.
- BCS (4/25) Jan 09 2006 I have a pre existing class that take a delegate in it's constructor and...
Is this valid int delegate(int) fn() { int ret(int i) // function doesn't use context pointer { return i+1; } return &ret; // returns delegate to _local_ nested function }
Jan 09 2006
BCS wrote:Is this valid int delegate(int) fn() { int ret(int i) // function doesn't use context pointer { return i+1; } return &ret; // returns delegate to _local_ nested function }I'm not so certain that nested functions are truly local (as in local variables) even though their style of declaration causes them to appear so. If you look at the assembler output, the declaration appears much like a global function, or else it used to, I think. I haven't checked again recently. The difference, of course, is that nested functions have access to their enclosing scopes local variables. As long as you don't access those, I imagine the above is possible. Does your dmd compiler allow this? -JJR
Jan 09 2006
John Reimer wrote:BCS wrote:Yes, the function works fine with DMD. However I'm not shure that it is even legal to do this. As a sinde note, what is the best way to use a regular function where a delagate is needed? e.i. given: int fn(int delegate(int)); int use_me(int); needed: call fn w/ use_me.Is this valid int delegate(int) fn() { int ret(int i) // function doesn't use context pointer { return i+1; } return &ret; // returns delegate to _local_ nested function }I'm not so certain that nested functions are truly local (as in local variables) even though their style of declaration causes them to appear so. If you look at the assembler output, the declaration appears much like a global function, or else it used to, I think. I haven't checked again recently. The difference, of course, is that nested functions have access to their enclosing scopes local variables. As long as you don't access those, I imagine the above is possible. Does your dmd compiler allow this? -JJR
Jan 09 2006
BCS wrote:As a sinde note, what is the best way to use a regular function where a delagate is needed? e.i. given: int fn(int delegate(int)); int use_me(int); needed: call fn w/ use_me.I'm not sure what the best way is, but this would probably work: -- Chris Sauls
Jan 09 2006
Chris Sauls wrote:BCS wrote:Thought of that (actually I was think a class, but struct would do better) How about a asm hack (in sudo code) fn2dg( void* fnp, ...) { copy fnp to a register manipulate stack ptr/frame ptr to remove it from the stack jmp fnp } usage (per above example) fn(&(cast(void*)(&use_me).fn2dg)); Yuck!! :P but it would be fast maybe something like this should be put in the D (the cast of a function to a delegate could do this implicitly)As a sinde note, what is the best way to use a regular function where a delagate is needed? e.i. given: int fn(int delegate(int)); int use_me(int); needed: call fn w/ use_me.I'm not sure what the best way is, but this would probably work: -- Chris Sauls
Jan 09 2006
"BCS" <BCS_member pathlink.com> wrote in message news:dpueko$6hc$1 digitaldaemon.com...Is this valid int delegate(int) fn() { int ret(int i) // function doesn't use context pointer { return i+1; } return &ret; // returns delegate to _local_ nested function }If it doesn't use the context pointer it should be fine. I suppose the reason for making it local is for the name scoping, then? Note you can use an anonymous delegate int delegate(int) fn() { return delegate int(int i){return i+1;}; }
Jan 09 2006
BCS wrote:Is this valid int delegate(int) fn() { int ret(int i) // function doesn't use context pointer { return i+1; } return &ret; // returns delegate to _local_ nested function }Technically, no. Why not use a function pointer instead? int function(int) fn() { static int ret( int i ) { return i + 1; } return &ret; }
Jan 09 2006
Sean Kelly wrote:BCS wrote:I totally missed the delegate/function pointer context here. -JJRIs this valid int delegate(int) fn() { int ret(int i) // function doesn't use context pointer { return i+1; } return &ret; // returns delegate to _local_ nested function }Technically, no. Why not use a function pointer instead? int function(int) fn() { static int ret( int i ) { return i + 1; } return &ret; }
Jan 09 2006
Sean Kelly wrote:BCS wrote:I have a pre existing class that take a delegate in it's constructor and I need to use an instance of this class after the function I'm creating it in returns. A function would work except that the code is expecting a delegateIs this valid int delegate(int) fn() { int ret(int i) // function doesn't use context pointer { return i+1; } return &ret; // returns delegate to _local_ nested function }Technically, no. Why not use a function pointer instead? int function(int) fn() { static int ret( int i ) { return i + 1; } return &ret; }
Jan 09 2006