www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can I convert function to delegate ?

reply Vova <Vova_member pathlink.com> writes:
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
parent reply Chris Sauls <ibisbasenji gmail.com> writes:
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
parent reply Vladimir <Vladimir_member pathlink.com> writes:
In article <d2eidl$1eev$1 digitaldaemon.com>, Chris Sauls says... 
 
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.
But what happens with delegate when it's stack frame is destroyed ?

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.  
I really do believe in it too.
 Meanwhile,  
might a delegate literal be a more
acceptable 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
parent reply "Carlos Santander B." <csantander619 gmail.com> writes:
Vladimir wrote:
 
 acceptable 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 ?
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 Bernal
Mar 30 2005
parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Carlos Santander B. wrote:
 Vladimir wrote:
 
 acceptable 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 ?
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.
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.
Mar 30 2005