Use UFCS for reducing dependencies
Bastiaan Veelo
Bastiaan at Veelo.net
Sun Jul 17 21:23:58 UTC 2022
On Sunday, 17 July 2022 at 16:25:52 UTC, Dave P. wrote:
> 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);
> }
>
> ```
It works if you add an overload:
```
void mutate2(Foo *foo)
{
mutate2(*foo);
}
```
More information about the Digitalmars-d
mailing list