Use UFCS for reducing dependencies
Dave P.
dave287091 at gmail.com
Sun Jul 17 16:25:52 UTC 2022
On Saturday, 16 July 2022 at 22:10:13 UTC, Hipreme wrote:
> [...]
I like this. What's annoying is that it doesn't work smoothly
with structs. Methods automatically deference pointers, but UFCS
functions don't.
```D
struct Foo {
int x;
void mutate(){
x++;
}
}
void mutate2(ref Foo foo){
foo.x++;
}
void main(){
Foo foo;
Foo* pfoo = &foo;
// Both ref and pointer work for methods
foo.mutate();
pfoo.mutate();
// Ref works for ufcs
foo.mutate2();
// But pointer doesn't work for UFCS
// Can't do this:
// pfoo.mutate2();
// Must do this:
(*pfoo).mutate2();
assert(foo.x == 4);
}
```
More information about the Digitalmars-d
mailing list