[Issue 23278] Can't pass alias member to a function
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Aug 9 15:11:41 UTC 2022
https://issues.dlang.org/show_bug.cgi?id=23278
--- Comment #4 from Marcelo Silva Nascimento Mancini <msnmancini at hotmail.com> ---
(In reply to RazvanN from comment #3)
> (In reply to Marcelo Silva Nascimento Mancini from comment #0)
> > This trivial code fails to compile with the error message:
> > `onlineapp.Test2.mxString!(oops).mxString` need `this` to access member
> > `mxString`
> >
> > ```d
> > class Test2
> > {
> > string oops;
> > mixin(mxString!(oops));
> > }
> > enum mxString(alias c)()
> > {
> > return "";
> > }
> > ```
> >
> > This should compile as there is no dependent usage on "this". Plus, the
> > message is obviously wrong as there is no member named `mxString`.
> >
> > A real use case for that is:
> > ```d
> > enum mxString(alias c)()
> > {
> > return typeof(c).stringof~" "~c.stringof~c.stringof~";";
> > }
> > ```
> > Which does not depends on "this", as some code like `mxString!(typeof(oops),
> > oops.stringof)` would work.
> >
> > Mixin template does not have this limitation, so, I believe a normal
> > function should not too.
>
> I don't really understand what you are trying to achieve here? mixin
> templates are used to insert declarations in a scope. If you want to mixin a
> function, why not declare mxString as:
>
> ```
> class Test2
> {
> string oops;
> mixin mxTempl!(oops);
>
> }
>
> mixin template mxTempl(alias c)
> {
> enum mxString()
> {
> return "";
> }
> }
> ```
>
> This is an enhancement request at best. However, I don't really see the
> point of this code. Why would you want to insert a publicly declared
> template in the scope of an aggregate? Why not just call the function
> template when it is needed with the appropriate template parameter?
Because I don't want to deal with mixin template overloads. When they are
executed, its overloads becomes inside them, so, I'm doing a code for using
string mixin instead. If I wish to generate the overloads using mixin template,
I must do `alias overload = mixed.overload;` for each function defined inside
it.
If I wish to iterate through its overloads and alias to them, I must do a named
mixin template, which will actually be inserted on its namespace. This can be
solved by doing another mixin template which actually executes a static foreach
over members from the named mixin template and generate the alias declarations.
This is literally fighting the language.
--
More information about the Digitalmars-d-bugs
mailing list