www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Converting function to delegate - was Re: How to know whether to use function or delegate

reply "Chris Miller" <chris dprogramming.com> writes:
On Sat, 22 Jul 2006 04:16:00 -0400, Walter Bright  =

<newshound digitalmars.com> wrote:

 Chris Nicholson-Sauls wrote:
 (If there is a hack or workaround to pass a delegate as a function  =
 pointer, I want to see that!)
The only way to do it is to generate a "thunk" of executable code at =
 runtime.
But wouldn't it be a lot easier to convert function to delegate? (even = without generating anything at runtime?) It seems to me that this is wha= t = most people want. I don't think it would be bad having just the one = conversion for a while (or even forever; delegates just have more = information, you wouldn't provide a thunk to store a long in an int). For example, people ask whether or not to use delegates or function = pointers for callbacks. They want the convenience of delegates, but they= = also don't want to force people to use nested functions or member = functions, but also want to allow using global and static functions. Whe= n = they ask me this question I tell them to go with delegates and give them= = this tip on how to make a "global delegate": struct Dummy { void myfunction() { ... } } Dummy funcs; mydelegate =3D &funcs.myfunction; // <-- Isn't there an easier way to directly convert a function to a delegate?
Jul 22 2006
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Chris Miller wrote:
 Isn't there an easier way to directly convert a function to a delegate?
Ehm, how about this: mydelegate = { somethingStatic(); }; Seems pretty easy to me. Gotta love that new delegate syntax. :) Gets a bit uglier once you add arguments though (especially if there are more than a few).
Jul 22 2006
parent reply BCS <BCS pathlink.com> writes:
Frits van Bommel wrote:
 Chris Miller wrote:
 
 Isn't there an easier way to directly convert a function to a delegate?
Ehm, how about this: mydelegate = { somethingStatic(); }; Seems pretty easy to me. Gotta love that new delegate syntax. :) Gets a bit uglier once you add arguments though (especially if there are more than a few).
for(int i=0; i<0x100000 i++) mydelegate(); lot-o-overhead. Copy all args to new stack frame, call-return, etc. Making a re-wrap-args and goto stub will save a bunch of time.
Jul 25 2006
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
BCS wrote:
 Frits van Bommel wrote:
 Chris Miller wrote:

 Isn't there an easier way to directly convert a function to a delegate?
Ehm, how about this: mydelegate = { somethingStatic(); }; Seems pretty easy to me. Gotta love that new delegate syntax. :) Gets a bit uglier once you add arguments though (especially if there are more than a few).
for(int i=0; i<0x100000 i++) mydelegate(); lot-o-overhead. Copy all args to new stack frame, call-return, etc. Making a re-wrap-args and goto stub will save a bunch of time.
I believe Chris asked for an /easy/ way, not /the most efficient/ way. If that's your thing though, I also posted some code that does that (for simple cases) in a thread called "Question about x86 delegate calling convention". Google can't find it on digitalmars.com just now (at least not my post), but here it is in the archives of the mailing list gateway at puremagic.com: http://lists.puremagic.com/pipermail/digitalmars-d/2006-July/005584.html
Jul 25 2006
parent BCS <BCS pathlink.com> writes:
Frits van Bommel wrote:
 BCS wrote:
 Frits van Bommel wrote:
 Chris Miller wrote:
 Isn't there an easier way to directly convert a function to a delegate?
Ehm, how about this: mydelegate = { somethingStatic(); };
for(int i=0; i<0x100000 i++) mydelegate();
[...]
 Making a re-wrap-args and goto stub will save a bunch of time.
I believe Chris asked for an /easy/ way, not /the most efficient/ way.
Ah, easy, as in "something simple I can do now, because I need to write the program". Not "how can D be changed to make this fast simple and elegant?" (e.i. fn->dg cast) Man, I've /got/ to quit (hypothetically) solving problems by (hypothetically) changing D. =P
Jul 25 2006