Lots of expressions don't work as mixin, and you need to expand the mixin to include the whole statement as a workaround.
Eg, doesn't work:
alias impl = mixin("Thing." ~ fieldName); // ERROR
Ugly workaround:
mixin("alias impl = Thing." ~ fieldName ~ ";");
This really inhibits readability when the code is even remotely complex.
I run into endless examples like this regularly. We can do better than this.
Comment #1 by b2.temp — 2019-07-16T15:02:25Z
Attempt : https://github.com/Basile-z/dmd/pull/3
But there are problems. The MixinExpression takes expressions as argument but among the stuff that are supposed to be aliased there are not only expressions, only types that look like expressions work. Concretely
A simple thing like
alias A1 = mixin("int");
Does not work and instead
alias Int = int;
alias A1 = mixin("Int");
must be used.
Otherwise the change is really simple (current work is less than an hour) so I may propose it anyway.
Comment #2 by bugzilla — 2019-07-23T09:14:29Z
> Lots of expressions don't work as mixin
They do. Compile mixins come in three forms:
1. declaration
2. statement
3. expression
The form they don't come in is as a type, which is what your example requires.
Also:
alias E = Expression;
where Expression is a general expression, doesn't work. For example,
int x;
alias Y = x + x;
doesn't work.
I supposed type mixins could be added, but I haven't seen a request for this before.
Comment #3 by turkeyman — 2019-07-23T20:22:34Z
Oh okay.
Well I run into this all the time. I hadn't noticed the pattern that it was always type expressions that caused the issue, and I just fellback to mixin the whole statement.
I'm quite tired of the workaround, and I don't enjoy having to explain it to people (since I can't give then an answer). Consider this such a request! :)
dlang/dmd pull request #10215 "fix Issue 20053 - add mixin types" was merged into master:
- a5d3c38852c9c660fb8a6bd929897f5aa3350603 by Walter Bright:
fix Issue 20053 - add mixin types
https://github.com/dlang/dmd/pull/10215
Comment #6 by dlang-bot — 2021-04-02T05:45:24Z
dlang/dmd pull request #12339 "[dmd-cxx] Backport fixes and trivial features from upstream dmd" was merged into dmd-cxx:
- a749391899e307be076a5202d9b0cc5e48240f4b by Walter Bright:
[dmd-cxx] fix Issue 20053 - add mixin types
https://github.com/dlang/dmd/pull/12339