From the D Blog: Crafting Self-Evident Code in D

Martyn martyn.developer at googlemail.com
Tue Oct 3 12:01:56 UTC 2023


On Tuesday, 3 October 2023 at 10:39:19 UTC, matheus wrote:
>
> Nice article but I think that I found a bug:
>
>     g(f(e(d(c(b(a))),3)));
>
>     a.b.c.d(3).e.f.g;
>
>> "That’s the equivalent, but execution flows clearly 
>> left-to-right. Is this an extreme example, or the norm?"
>
> Well I don't think they're equivalent:
>
>     g(f(e(d(c(b(a))),3)));
>
> I the first example "e" is receiving two arguments. While in 
> the latter "d" is being receiving whatever "c" returns and "3".
>
> Matheus.

Agreed. Even though I do like UFCS, I find the above confusing to 
follow despite being more pleasing to the eye. I had to break it 
down and, as Matheus already pointed out, looked incorrect.

This appears to be the correct code.

```d
int a() { return 1;  }
int b(int i) { return i+1; }
int c(int i) { return i+2; }
int d(int i) { return i+3; }
int e(int a, int b) { return a+b; }
int f(int i) { return i+4; }
int g(int i) { return i+5; }

void main()
{
     import std.stdio : writeln;
     auto r = g(f(e(d(c(b(a))),3)));
     auto r2 = a.b.c.d.e(3).f.g;

     writeln(r);    // 19
     writeln(r2);   // 19
}
```


More information about the Digitalmars-d-announce mailing list