www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - another question on hidden mixin names

reply 60rntogo <60rntogo gmail.com> writes:
I suspect that this is similar to the issue I asked about here: 
https://forum.dlang.org/post/vukxaqprjbyrdpioukwa forum.dlang.org, but I can't
figure it out.

This compiles:

---
private mixin template assign(string op_)
{
   ref auto opOpAssign(string op, this RHS)(RHS rhs) if (op == op_)
   {
     mixin("x " ~ op ~ "= rhs.x;");
     return this;
   }
}

struct V
{
   int x;

   mixin assign!"+";
   // mixin assign!"-";
}

unittest
{
   auto v = V(2);
   v += v;
   assert(v.x == 4);
}
---

However, if I uncomment the second mixin, there is an error "v is 
not a scalar, it is a V". I guess I somehow need to merge these 
overloads, but I don't know how.
Sep 17 2020
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Thursday, 17 September 2020 at 21:05:59 UTC, 60rntogo wrote:
 struct V
 {
   int x;

   mixin assign!"+";
   // mixin assign!"-";
 }

 However, if I uncomment the second mixin, there is an error "v 
 is not a scalar, it is a V". I guess I somehow need to merge 
 these overloads, but I don't know how.
Usually, that would be: struct V { int x; mixin assign!"+" a; mixin assign!"-" b; alias opOpAssign = a.opOpAssign; alias opOpAssign = b.opOpAssign; } However, I can't seem to get that working. It seems to be an instance of this issue: https://issues.dlang.org/show_bug.cgi?id=18118 Note that explicitly calling the methods does work: v.opOpAssign!"-"(v); v.opOpAssign!"+"(v); I don't know any good workarounds for this, you'll probably have to write a separate opOpAssign method for V that performs the forwarding to the mixed-in methods. This is, to put it very nicely, not an optimal solution. btw, I'm somewhat surprised by your use of a template this parameter (https://dlang.org/spec/template.html#template_this_parameter). Generally this will work, but you're probably better off with ref auto opOpAssign(string op)(typeof(this) rhs) if (op == op_) -- Simen
Sep 17 2020
parent 60rntogo <60rntogo gmail.com> writes:
On Thursday, 17 September 2020 at 22:07:54 UTC, Simen Kjærås 
wrote:
 Usually, that would be:

 struct V {
     int x;

     mixin assign!"+" a;
     mixin assign!"-" b;
     alias opOpAssign = a.opOpAssign;
     alias opOpAssign = b.opOpAssign;
 }

 However, I can't seem to get that working. It seems to be an 
 instance of this issue:
 https://issues.dlang.org/show_bug.cgi?id=18118
Yes, I tried that. It didn't work so I assumed that it must have been wrong. Is this a bug in the compiler or an issue in the language specification?
 btw, I'm somewhat surprised by your use of a template this 
 parameter 
 (https://dlang.org/spec/template.html#template_this_parameter). 
 Generally this will work, but you're probably better off with

     ref auto opOpAssign(string op)(typeof(this) rhs) if (op == 
 op_)
I learned it from here: http://ddili.org/ders/d.en/mixin.html, could you elaborate on why this is not advisable?
Sep 17 2020