`alias x = v.x;` not working in struct bodies?

FeepingCreature feepingcreature at gmail.com
Sun Jan 21 19:45:03 UTC 2024


On Sunday, 21 January 2024 at 15:03:15 UTC, Basile B. wrote:
> On Sunday, 21 January 2024 at 13:18:07 UTC, FeepingCreature 
> wrote:
>> I have a hard time understanding what problem you're 
>> describing, but it's probably fine? Expression aliases are 
>> evaluated in the context of the declaration site, not the call 
>> site. This doesn't involve the grammar at all.
>
> I'm talking about that:
>
> ```d
> struct B
> {
>     int i;
> }
>
> struct A
> {
>     B b;
>     alias cmp = (b.i == 0);
> }
>
> void main()
> {
>     A a;
>     if (a.cmp) {} // a.(b.i == 0)
>                   // but user wants (a.b.i) == 0
> }
> ```
>
> You cant just rewrite them as DotExp otherwise operands are not 
> evaluated properly, or like in the example, invalid expressions 
> are generated.

`if (a.(b.i == 0))` is actually syntactically correct in Neat and 
will result in the same outcome. :-)

But I think you're still looking at it at too high a level. 
`a.cmp` is not *rewritten* as `a.(b.i == 0)`, rather 
syntactically it stays as `a.cmp`, and the *lookup* for "cmp" 
results in the *evaluation* of `(b.i == 0)`, or `(this.b.i == 
0)`, in the context of `thisptr = &a`. It's not a term rewriting 
system.


More information about the Digitalmars-d mailing list