UFCS and delegates
Rikki Cattermole
alphaglosined at gmail.com
Sat Mar 15 02:13:45 PDT 2014
On Saturday, 15 March 2014 at 08:33:13 UTC, Steve Teale wrote:
> Doesn't the logic of UFCS rather suggest that this should
> compile?
>
> struct A
> {
> int m;
> void foo(int n) { m += n; }
> }
>
> void bar(ref A a, int n)
> {
> a.foo(n*n);
> }
>
> void delegate(int) dg = &bar;
>
> void main()
> {
> A a;
> a.bar(3);
> dg(3);
> assert(a.m == 18);
> }
This yes:
struct A
{
int m;
void foo(int n) { m += n; }
void bar(int n) {
foo(n*n);
}
}
void main()
{
A a;
void delegate(int) dg = &a.bar;
a.bar(3);
dg(3);
assert(a.m == 18);
}
yours no.
Because a delegate stores a context ptr aka this. As well as a
function pointer. What you were doing meant that no content
pointer was being stored. Essentially it was just a function
pointer without the first argument added.
More information about the Digitalmars-d
mailing list