digitalmars.D.learn - Can I convert function to delegate ?
- Vova (7/7) Mar 30 2005 Hello All !
- Chris Sauls (18/27) Mar 30 2005 I believe its because delegates contain a referance to the class
- Vladimir (23/53) Mar 30 2005 argument, but I want to pass
- Carlos Santander B. (6/31) Mar 30 2005 Say you're doing this inside a class non-static member function. Which
- Russ Lewis (10/35) Mar 30 2005 Well, if you're casting a function pointer to a delegate, then it
Hello All ! I have function that accepts delegate as it's argument, but I want to pass ordinary function to it. Of couse I can create class which encapsulates that function, but it seems to be an ugly hack (that was good in C++). I'm wondering why cast(delegate int(int)) myfunc does not work.
Mar 30 2005
Vova wrote:Hello All ! I have function that accepts delegate as it's argument, but I want to pass ordinary function to it. Of couse I can create class which encapsulates that function, but it seems to be an ugly hack (that was good in C++). I'm wondering why cast(delegate int(int)) myfunc does not work.I believe its because delegates contain a referance to the class instance or stack frame where they are born. Casting a function to a delegate would leave one with a null referance, and that is apparently a "bad thing". I do believe Walter intends to unify functions and delegates in the future though, probably by just allowing the null referance and making a special case of it, or some such. Meanwhile, might a delegate literal be a more acceptable hack? Something like: -- Chris Sauls
Mar 30 2005
In article <d2eidl$1eev$1 digitaldaemon.com>, Chris Sauls says...Vovawrote:argument, but I want to passHello All ! I have function that accepts delegate as it'sclass which encapsulates thatordinary function to it. Of couse I can create(that was good in C++).function, but it seems to be an ugly hackmyfuncI'm wondering why cast(delegate int(int))contain a referance to the classdoes not work.I believe its because delegatesinstance or stack frame where they are born.But what happens with delegate when it's stack frame is destroyed ?Casting a function to adelegate would leave one with a null referance, andthat is apparently a"bad thing". I do believe Walter intends to unifyfunctions anddelegates in the future though, probably by just allowing thenullreferance and making a special case of it, or some such.I really do believe in it too.Meanwhile, might a delegate literal be a moreacceptable hack? Something like:something (int delegate(int) dg) { ... }(int a) {Yes, it look quite good. Thanks a lot. But why not to make casting function to delegate to do the same thing ?
Mar 30 2005
Vladimir wrote:acceptable hack? Something like:Say you're doing this inside a class non-static member function. Which frame should be passed: the current function or the current object? That's the only problem I can see right now. _______________________ Carlos Santander Bernalsomething (int delegate(int) dg) { ... }(int a) {Yes, it look quite good. Thanks a lot. But why not to make casting function to delegate to do the same thing ?
Mar 30 2005
Carlos Santander B. wrote:Vladimir wrote:Well, if you're casting a function pointer to a delegate, then it doesn't matter. Remember, if you have a function pointer to a class member, then it must be a static class member, and so there isn't any 'current object' that would apply to it. While we're on the subject, I'll note that it is currently not possible to declare a delegate literal where the object pointer is anything other than the stack frame. I've proposed a syntax to Walter where you could pass an object (or struct) pointer as the the pointer, but he doesn't seem to be interested in it, at least not yet.acceptable hack? Something like:Say you're doing this inside a class non-static member function. Which frame should be passed: the current function or the current object? That's the only problem I can see right now.something (int delegate(int) dg) { ... }(int a) {Yes, it look quite good. Thanks a lot. But why not to make casting function to delegate to do the same thing ?
Mar 30 2005