www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - `Alias this` to a mixed in property

reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
Hi,

`Alias this` to mixed in properties does not seem to work, see 
below. If you think it should, I'll file an issue. Otherwise: can 
this be made to work somehow?

Interestingly, if you uncomment either the mixin getter or setter 
(row 36 or 37) and its corresponding use in `main`, then the 
remaining property works. Does the compiler detect identical 
names and then does some additional mangling which messes with my 
`alias this` maybe?

```
import std.stdio;

// version = manual; // Manually written property works, 
obviously.

mixin template getter()
{
      property int p()
     {
         writeln(__LINE__, " mixin getter");
         return 3;
     }
}
mixin template setter(T)
{
      property int p(T arg)
     {
         writeln(__LINE__, " mixin setter ", typeid(T), ' ',  arg);
         return 4;
     }
}
struct S
{
     version (manual) {
          property int p()
         {
             writeln(__LINE__, " manual getter");
             return 3;
         }
          property int p(int arg)
         {
             writeln(__LINE__, " manual setter int ",  arg);
             return 4;
         }
     }
     else {
         mixin getter;            // row 36
         mixin setter!int;        // row 37
     }
     alias p this;
}


void main(string[] args)
{
     S s;
     s = 7;
     int i = s;
}
```

Error: cannot implicitly convert expression s of type S to int.
Jan 24 2018
parent reply ag0aep6g <anonymous example.com> writes:
On 01/24/2018 02:24 PM, Bastiaan Veelo wrote:
 `Alias this` to mixed in properties does not seem to work, see below. If 
 you think it should, I'll file an issue. Otherwise: can this be made to 
 work somehow?
Not supposed to work as it is. The spec says that you cannot make an overload set just by mixing in multiple functions/methods with the same name. Instead, you have to do it like this: ---- mixin getter g; mixin setter!int s; alias p = g.p; alias p = s.p; ---- https://dlang.org/spec/template-mixin.html#mixin_scope
Jan 24 2018
parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Wednesday, 24 January 2018 at 14:21:42 UTC, ag0aep6g wrote:
 The spec says that you cannot make an overload set just by 
 mixing in multiple functions/methods with the same name. 
 Instead, you have to do it like this:

 ----
 mixin getter g;
 mixin setter!int s;

 alias p = g.p;
 alias p = s.p;
 ----

 https://dlang.org/spec/template-mixin.html#mixin_scope
Thanks a lot! I didn't know you could do overloads by way of multiple aliases with the same name. I meant to use this for mixing in multiple instantiations generated from a static foreach over an AliasSeq of types, but generating unique identifiers poses an extra challenge. I may go for string mixin's instead, which I just discovered do work: ``` import std.stdio; enum getter = ` property int p() { writeln(__LINE__, " mixin getter"); return 3; } `; string setter(string T) pure { return ` property int p(` ~ T ~ ` arg) { writeln(__LINE__, " mixin setter ` ~ T ~ `" , arg); return 4; } `; } struct S { mixin(getter); mixin(setter("int")); alias p this; } void main(string[] args) { S s; s = 7; int i = s; } ```
Jan 24 2018