Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
March 30, 2005 Can I convert function to delegate ? | ||||
---|---|---|---|---|
| ||||
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. |
March 30, 2005 Re: Can I convert function to delegate ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vova | 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:
# int myfunc (int i) { ... }
#
# int something (int delegate(int) dg) { ... }
#
# something(
# delegate int (int a) {
# return myfunc(a);
# }
# );
-- Chris Sauls
|
March 30, 2005 Re: Can I convert function to delegate ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Sauls | 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: > ># int myfunc (int i) { ... } # # int something (int delegate(int) dg) { ... } ># # something( # delegate int (int a) { ># return myfunc(a); # } # ); Yes, it look quite good. Thanks a lot. But why not to make casting function to delegate to do the same thing ? |
March 30, 2005 Re: Can I convert function to delegate ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir | Vladimir wrote:
>
> acceptable hack? Something like:
>
>># int myfunc (int i) { ... } # # int
>
> something (int delegate(int) dg) { ... }
>
>># # something( # delegate int
>
> (int a) {
>
>># return myfunc(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
|
March 30, 2005 Re: Can I convert function to delegate ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | Carlos Santander B. wrote:
> Vladimir wrote:
>
>>
>> acceptable hack? Something like:
>>
>>> # int myfunc (int i) { ... } # # int
>>
>>
>> something (int delegate(int) dg) { ... }
>>
>>> # # something( # delegate int
>>
>>
>> (int a) {
>>
>>> # return myfunc(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.
|
Copyright © 1999-2021 by the D Language Foundation