Thread overview
delegate with optional parameters
Apr 02, 2017
Inquie
Apr 02, 2017
Basile B.
Apr 02, 2017
Inquie
Apr 02, 2017
Basile B.
Apr 02, 2017
Inquie
Apr 03, 2017
Ali Çehreli
Apr 03, 2017
Inquie
Apr 03, 2017
Rene Zwanenburg
April 02, 2017
is it possible to create a delegate that takes an optional number of parameters and/or return type?

T delegate(S...)(S) special_delegate;

I guess this is impossible?
April 02, 2017
On Sunday, 2 April 2017 at 19:24:14 UTC, Inquie wrote:
> is it possible to create a delegate that takes an optional number of parameters and/or return type?
>
> T delegate(S...)(S) special_delegate;
>
> I guess this is impossible?

alias Dg(Return, Params...) = Return delegate(Params);

Dg!(int,float, string) myDg;
April 02, 2017
On Sunday, 2 April 2017 at 20:02:56 UTC, Basile B. wrote:
> On Sunday, 2 April 2017 at 19:24:14 UTC, Inquie wrote:
>> is it possible to create a delegate that takes an optional number of parameters and/or return type?
>>
>> T delegate(S...)(S) special_delegate;
>>
>> I guess this is impossible?
>
> alias Dg(Return, Params...) = Return delegate(Params);
>
> Dg!(int,float, string) myDg;

What I mean is that I want to be able to overload delegates like one can do with normal members.
April 02, 2017
On Sunday, 2 April 2017 at 20:48:09 UTC, Inquie wrote:
> On Sunday, 2 April 2017 at 20:02:56 UTC, Basile B. wrote:
>> On Sunday, 2 April 2017 at 19:24:14 UTC, Inquie wrote:
>>> is it possible to create a delegate that takes an optional number of parameters and/or return type?
>>>
>>> T delegate(S...)(S) special_delegate;
>>>
>>> I guess this is impossible?
>>
>> alias Dg(Return, Params...) = Return delegate(Params);
>>
>> Dg!(int,float, string) myDg;
>
> What I mean is that I want to be able to overload delegates like one can do with normal members.

Show a usage, someone certainly propose a pattern that does the job.
April 02, 2017
On Sunday, 2 April 2017 at 21:47:55 UTC, Basile B. wrote:
> On Sunday, 2 April 2017 at 20:48:09 UTC, Inquie wrote:
>> On Sunday, 2 April 2017 at 20:02:56 UTC, Basile B. wrote:
>>> On Sunday, 2 April 2017 at 19:24:14 UTC, Inquie wrote:
>>>> is it possible to create a delegate that takes an optional number of parameters and/or return type?
>>>>
>>>> T delegate(S...)(S) special_delegate;
>>>>
>>>> I guess this is impossible?
>>>
>>> alias Dg(Return, Params...) = Return delegate(Params);
>>>
>>> Dg!(int,float, string) myDg;
>>
>> What I mean is that I want to be able to overload delegates like one can do with normal members.
>
> Show a usage, someone certainly propose a pattern that does the job.

int delegate() f;
void delegate(int) f;

These are effectively overload methods, but my guess is that D won't support it like overloads.
e.g.,

int f();
void f(int);

April 02, 2017
On 04/02/2017 03:24 PM, Inquie wrote:

>> Show a usage, someone certainly propose a pattern that does the job.
>
> int delegate() f;
> void delegate(int) f;

That won't work because both of those are variables and variables don't have overloading.

> These are effectively overload methods, but my guess is that D won't
> support it like overloads.
> e.g.,
>
> int f();
> void f(int);

Yep, both 'f' are functions there.

I'm having difficulty understanding your actual need as well. :/ A guess: It is possible to determine delegate parameter list at compile time like std.concurrency.receive does.

Ali

April 03, 2017
On Monday, 3 April 2017 at 03:08:22 UTC, Ali Çehreli wrote:
> On 04/02/2017 03:24 PM, Inquie wrote:
>
> >> Show a usage, someone certainly propose a pattern that does
> the job.
> >
> > int delegate() f;
> > void delegate(int) f;
>
> That won't work because both of those are variables and variables don't have overloading.
>
> > These are effectively overload methods, but my guess is that
> D won't
> > support it like overloads.
> > e.g.,
> >
> > int f();
> > void f(int);
>
> Yep, both 'f' are functions there.
>
> I'm having difficulty understanding your actual need as well. :/ A guess: It is possible to determine delegate parameter list at compile time like std.concurrency.receive does.
>
> Ali

Yes, but they are really not any different. They only look different. A field can be a function just like a method because they look exactly the same except on is in a vtable and the other is in the fields memory. But both point functions.

The only difference is that we can't write to the vtable to overwrite a value easily but we can to a delegate(no hackery).

So, it would be nice to be able to overload them. Effectively we can extend the vtable out in to the fields. (it would require a bit of work to make it work identical to a class, but it could, the outside world would know no difference).

If one wants: It essentially allows for methods to be modifiable at run time(something that classes can't do without unsafely hacking the vtable) and that is exactly why I have used it, but overloading causes a problem because only the name collides yet it works with the methods case but not the field delegates(a field delegate is essentially a method, is the point(for functional usage)).


April 03, 2017
On Monday, 3 April 2017 at 05:00:15 UTC, Inquie wrote:
> Yes, but they are really not any different. They only look different. A field can be a function just like a method because they look exactly the same except on is in a vtable and the other is in the fields memory. But both point functions.

It should be possible to create a wrapper struct around your 'overloads' with an opDispatch which selects the right delegate.