January 29, 2008
Leandro Lucarella wrote:
> Walter Bright, el 28 de enero a las 18:51 me escribiste:
>> Edward Diener wrote:
>>> Perhaps there are some implementation issues regarding this which
>>> I do not understand but these should be overcome in order to
>>> present a single interface to the callable concept. Perhaps this
>>> has been discussed ad nauseam on this NG already, so please
>>> correct me if I do not understand all the ramifications of
>>> uniting all callable types under the single idea of 'delegate'
>>> while perhaps keeping 'function' around merely for backward
>>> compatibility with old code until eventually it is dropped.
>> To merge the two requires the generation of thunks at runtime to
>> manipulate the parameters and calling convention. It's doable, but
>> not simple.
> 
> And please, don't forget we need plain function pointers to interact
> with C.

Yes, we do need that. But plain function pointers seem to get more and
more relegated to only being for that, much as char* pointers have given
way to char[], and you only see char* when interfacing with C.
January 29, 2008
"Walter Bright" <newshound1@digitalmars.com> wrote in message news:fnmrjo$109m$1@digitalmars.com...
> Lionello Lunesu wrote:
>> Walter Bright wrote:
>>> To merge the two requires the generation of thunks at runtime to manipulate the parameters and calling convention. It's doable, but not simple.
>>
>> Perhaps allowing a delegate to point to a free function is enough to make people happy?
>>
>> It sure would please me ;)
>
> Me too, but it still changes the calling convention, and still needs a thunk.

I don't understand why this is so difficult.  There already exists a function template that can be used to convert a function pointer to a delegate.  BCS showed it to me a while back.  Why not just have DMD call such a function to convert function pointers to delegates?

T delegate(A) Fn2Dg(T, A...)(T function(A) f)
{
  struct tmp { T ret(A args){ return (cast(T function(A))this)(args); } };
  return &(cast(tmp*)f).ret;
}

-Craig


January 30, 2008
Lionello Lunesu wrote:
> Walter Bright wrote:
>> Edward Diener wrote:
>>> Perhaps there are some implementation issues regarding this which I do
>>> not understand but these should be overcome in order to present a single
>>> interface to the callable concept. Perhaps this has been
>>> discussed ad nauseam on this NG already, so please correct me if I do
>>> not understand all the ramifications of uniting all callable types under
>>> the single idea of 'delegate' while perhaps keeping 'function' around
>>> merely for backward compatibility with old code until eventually it is
>>> dropped.
>>
>> To merge the two requires the generation of thunks at runtime to manipulate the parameters and calling convention. It's doable, but not simple.
> 
> Perhaps allowing a delegate to point to a free function is enough to make people happy?
> 
> It sure would please me ;)

Yes, that is the essence of my suggestion.

There is no need to have a separate 'function' as callback since a 'delegate' should be able to encompass any non-member function also. After all if a 'delegate' needs both an object pointer and and a ( member ) function pointer, and a 'function' just needs a function pointer, then for a delegate encompassing a free function the object pointer is just null.

As I originally stated, setting up a callback does NOT care whether what is called is a member function or a non-member function, any more than an event ( a signal ) cares whether event handlers ( slots  ) are member functions or free functions. Furthermore, with both 'function' and 'delegate' representing callables, setting up a callback, or an event, means having to cater to two different types of callables. This just complicates what should be a single concept and implementation.

I am sure Walter understands all this and the difficulty in having a single callback has nothing to do with the validity of the idea and everything to do with the implementation. Yet clearly it is a much superior design to the separate 'function'/'delegate' dichotomy.
January 30, 2008
Leandro Lucarella wrote:
> Walter Bright, el 28 de enero a las 18:51 me escribiste:
>> Edward Diener wrote:
>>> Perhaps there are some implementation issues regarding this which I do
>>> not understand but these should be overcome in order to present a single
>>> interface to the callable concept. Perhaps this has been
>>> discussed ad nauseam on this NG already, so please correct me if I do
>>> not understand all the ramifications of uniting all callable types under
>>> the single idea of 'delegate' while perhaps keeping 'function' around
>>> merely for backward compatibility with old code until eventually it is
>>> dropped.
>> To merge the two requires the generation of thunks at runtime to manipulate the parameters and calling convention. It's doable, but not simple.
> 
> And please, don't forget we need plain function pointers to interact with
> C.

You might need plain function pointers but you do not need 'function'. A 'delegate' which encompassed a 'function', with the object pointer part of the delegate being null in that particle case, should be implicitly convertible to a C function pointer.
January 30, 2008
On 1/30/08, Edward Diener <eddielee_no_spam_here@tropicsoft.com> wrote:
> You might need plain function pointers but you do not need 'function'. A 'delegate' which encompassed a 'function', with the object pointer part of the delegate being null in that particle case, should be implicitly convertible to a C function pointer.

In general, the compiler cannot tell at compile time whether or a not a value is null at runtime (though in an expression like &f it obviously can), so I don't think it can be quite that automatic.

Nonetheless, I'm inclined to agree that we probably don't need the "function" keyword at all.

    delegate f;
    oldFashionedCFunction( f.funcptr );
January 30, 2008
Janice Caron wrote:
> On 1/30/08, Edward Diener <eddielee_no_spam_here@tropicsoft.com> wrote:
>> You might need plain function pointers but you do not need 'function'. A
>> 'delegate' which encompassed a 'function', with the object pointer part
>> of the delegate being null in that particle case, should be implicitly
>> convertible to a C function pointer.
> 
> In general, the compiler cannot tell at compile time whether or a not
> a value is null at runtime (though in an expression like &f it
> obviously can), so I don't think it can be quite that automatic.
> 
> Nonetheless, I'm inclined to agree that we probably don't need the
> "function" keyword at all.
> 
>     delegate f;
>     oldFashionedCFunction( f.funcptr );

But we still need typedefs and declarations. How do you declare oldFashionedCFunction() ?

At the moment, you can have an extern(C) function pointer, ... but what is an extern(C) delegate ?
AFAIK, all delegates use the D calling convention. This is not true for functions. Clearly several things would need to change.
January 30, 2008
On Jan 30, 2008 8:16 AM, Don Clugston <dac@nospam.com.au> wrote:
> How do you declare
> oldFashionedCFunction() ?

The old fashioned way?

    extern(C) void (*callback)(int,int);


> what is an extern(C) delegate ?

How can that even make sense? C does not have delegates.
January 30, 2008
On Jan 30, 2008 8:36 AM, Janice Caron <caron800@googlemail.com> wrote:
> On Jan 30, 2008 8:16 AM, Don Clugston <dac@nospam.com.au> wrote:
> > How do you declare
> > oldFashionedCFunction() ?
>
> The old fashioned way?
>
>     extern(C) void (*callback)(int,int);

Or, for any delegate dg,

    typeof(dg.funcptr) callback;
January 30, 2008
Janice Caron wrote:
> On Jan 30, 2008 8:36 AM, Janice Caron <caron800@googlemail.com> wrote:
>> On Jan 30, 2008 8:16 AM, Don Clugston <dac@nospam.com.au> wrote:
>>> How do you declare
>>> oldFashionedCFunction() ?
>> The old fashioned way?
>>
>>     extern(C) void (*callback)(int,int);

That's just 'function', using the hideous C syntax.

> Or, for any delegate dg,
> 
>     typeof(dg.funcptr) callback;

That only works if you can have extern(C), extern(Windows) delegates.

The problem is that, because of calling conventions, not all function pointers can be expressed by dg.funcptr.

The existing delegates are therefore not a superset of function pointers.
January 30, 2008
On Jan 30, 2008 11:17 AM, Don Clugston <dac@nospam.com.au> wrote:
> >>     extern(C) void (*callback)(int,int);
>
> That's just 'function', using the hideous C syntax.

Yes, that is precisely my suggestion. I see that as an advantage, not a disadvantage, because it would encourage people to use "delegate", unless they absolutely couldn't.

All I'm saying is, if you're working in D alone, you can get along just fine using "delegate" instead of "function", and therefore the keyword "function" is unnecessary.

On the other hand, if you want to interface with C, then you shouldn't be surprised if you have to use hideous C syntax.

So we /could/ ditch "function" and have one fewer keyword.