[Issue 5710] cannot use delegates as parameters to non-global template

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon May 6 15:20:24 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=5710



--- Comment #19 from Diggory <diggsey at googlemail.com> 2013-05-06 15:20:20 PDT ---
(In reply to comment #18)
> (In reply to comment #17)
> > Kenji's solution means two context pointer delegates are just normal delegates
> > (still a function pointer and a context pointer) so they can still be passed
> > around as usual.
> 
> OK, but how does it devolve to a 'this' pointer for the class?  In other words,
> doesn't the compiler have to treat 'this' in a special way while inside
> doStuff?
> 
> Is that OK?  I guess it's not too important, but it would be a new ABI call
> type, no?
> 
> I wonder if it wouldn't be better to push 'foo' and then the stack frame on to
> the stack, and then the call to doStuff would look like:
> 
> uint doStuff(uint a, uint b, void* this)
> {
>     // *(&(this) + 0) points the object Foo
>     // *(&(this) + 1) points the stack frame of main()
> 
>     //return fun(a, b);
>     return fun(a, b, *(&this + 1));
> }
> 
> Would that not work?  I'd actually rather just see another parameter:
> 
> uint doStuff(uint a, uint b, void *this1, void *this)
> {
>    return fun(a, b, this1);
> }
> 
> That is simpler for me to understand, I don't see why the compiler can't do
> this.

How the parameters are actually passed is fairly irrelevant, the main problem
which needs to be solved is how to store two context pointers in a delegate,
when currently delegates look like this:
struct dg {
    void* contextPtr;
    void* funcPtr;
}

If you add an extra context pointer, you have to either make all delegates
bigger:
struct dg {
    void* contextPtr1;
    void* contextPtr2;
    void* funcPtr;
}

Or have two or more types of delegates which cannot be used interchangeably (in
which case they're fairly useless as delegates!)

Kenji is suggesting something like this:
struct dg {
    void* contextPtr;
    void* funcPtr;
}
struct multiContext {
    void* contextPtr1;
    void* contextPtr2;
}

The "multiContext" struct is completely invisible to anyone using the delegate,
and it doesn't affect in the slightest how the delegate is called, you're still
passing in one context pointer.

The decoding into two context pointers is handled by the callee, because the
callee knows how many context pointers it should have. (Think about it, how can
a function not know how many context pointers it needs!)

It should be possible to call a delegate without having to know anything about
the receiver other than what arguments it takes.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list