UFCS functions with both pointers and refs
Dave P.
dave287091 at gmail.com
Sun Dec 13 18:31:54 UTC 2020
If I define a method on a type, then I can call it both through a
pointer and
through a reference and the compiler does the right thing. Eg:
struct Foo {
int x;
void fooey(){
x++;
}
void report(){
printf("%d\n", x);
}
}
int main(){
Foo f;
f.fooey;
Foo* pf = &f;
pf.fooey;
f.report; // prints 2
return 0;
}
However, if I define it as a free function and try to invoke it
via UFCS,
it seems like I have to define it twice.
struct Foo {
int x;
void report(){
printf("%d\n", x);
}
}
void fooey(Foo* f){
f.x++;
}
void fooey(ref Foo f){
f.x++;
}
int main(){
Foo f;
f.fooey;
Foo* pf = &f;
pf.fooey;
f.report; // prints 2
return 0;
}
Am I missing something or is this just how it has to work
generally?
Do I have to write both and have one forward to the other for more
complicated functions?
More information about the Digitalmars-d-learn
mailing list