Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aliasing for fields does not work #20610

Closed
WalterBright opened this issue Dec 28, 2024 · 8 comments · Fixed by #20611
Closed

aliasing for fields does not work #20610

WalterBright opened this issue Dec 28, 2024 · 8 comments · Fixed by #20611
Labels

Comments

@WalterBright
Copy link
Member

struct T {
    int k,i = 2;
}

struct S {
    int x;
    T t;
    alias ti = t.i;
}

int main() {
    T t = T(1, 2);
    S s;
    assert(s.ti == 2); // Error: accessing non-static variable `i` requires an instance of `T`
    return 0;
}

s.ti should be equivalent to s.t.i

@WalterBright
Copy link
Member Author

fix: #20611

@WalterBright WalterBright linked a pull request Dec 28, 2024 that will close this issue
@TurkeyMan
Copy link
Contributor

Wonderful!

Will this also address elements of an AliasSeq?

alias seq = AliasSeq!(x, y, z);
alias x = seq[0];

@WalterBright
Copy link
Member Author

Unfortunately, not. Wanna submit a bug report?

WalterBright added a commit to WalterBright/dmd that referenced this issue Dec 28, 2024
@TurkeyMan
Copy link
Contributor

TurkeyMan commented Dec 28, 2024

In general, I think it might be really useful to make alias work generally with expressions.
Since I think the reason that alias x = seq[0] doesn't work, is because it is effectively aliasing a [] operator expression? There are so many situations that fall into this general category where alias fails; is it out of the question for alias to be able to carry an expression?

Although, my understanding was that the . operator which accesses a member was also a member-access-expression, and that's why alias didn't work on that... which is why I thought this might have addressed the AliasSeq situation too...?

@TurkeyMan
Copy link
Contributor

Unfortunately, not. Wanna submit a bug report?

#20612

@WalterBright
Copy link
Member Author

Ehhxcellent!

@WalterBright
Copy link
Member Author

There are already ambiguities that I struggled to account for in fixing this issue. More aliasing means more trouble with this. Aliasing tries to do too much!

Aliasing an expression is better done with a lambda, where it is well defined. The lambda will get inlined, and all will be well.

ImportC creates lambdas out of macros that look like expressions. This works quite well.

@TurkeyMan
Copy link
Contributor

Aliases are more often used to refer to types or symbols... In the cases of interest if a lambda was workable, there wouldn't be anything to talk about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants