Feature Request: Change the delegate type to void[] data instead of void *ptr.

Daniel Keep daniel.keep.lists at gmail.com
Thu Oct 4 00:04:44 PDT 2007



downs wrote:
> This could be made backwards compatible by the introduction of a void
> *ptr() { return data.ptr; }
> 
> If the delegate is of a method, data references the object's data.
> If the delegate is an inline function or nested function, data
> references *the stack space in all the superior functions that it uses*.
> 
> 
> Disadvantages: Delegates take size_t.sizeof bytes more space in RAM.
> Advantages: I'll let the following function speak for itself.
>> R delegate(T) close(R, T...)(R delegate(T) dg)
>> {
>>   typeof(dg) res=dg;
>>   res.data=res.data.dup; // eventually moves area of stack into RAM
>>   return res;
>> }
> 
> What do you think?
>  --downs

What about this?  Leave delegates as they are, but introduce a new
closure class of types.  In effect, these would be:

struct closure(returnT, argT...)
{
    void* func;
    void[] data;

    returnT delegate(argT) opImplicitCast()
    {
        returnT delegate(argT) result;
        result.func = this.func;
        result.ptr = this.data.ptr;
        return result;
    }

    typeof(*this) dup()
    {
        typeof(*this) result;
        result.func = this.func;
        result.data = this.data.dup;
        return result;
    }
}

And appropriate opCall overloads, yadda yadda.  Now, instead of delegate
literals, you have *closure* literals, and taking the address of a
non-globally scoped function will result in a closure.

The implicit cast allows closures to be transparently used as delegates
instead, so all existing code that deals with delegates explicitly
should continue to work.

I think the important thing is that it's quite likely the *only* piece
of code that cares about the full slice of the stack is the code where
the delegate was created; other things likely just want to call the
delegate, so they probably don't care *how* it works, so long as it does.

Plus, this means we get to keep our 8-byte delegates. :)

	-- Daniel



More information about the Digitalmars-d mailing list