delegate with optional parameters

Inquie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 2 22:00:15 PDT 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)).




More information about the Digitalmars-d-learn mailing list