On 6/5/23 11:33 AM, Basile B. wrote:
On Monday, 5 June 2023 at 15:13:43 UTC, Basile B. wrote:
On Monday, 5 June 2023 at 13:57:20 UTC, Ki Rill wrote:
How do I generate `setX` methods for all private mutable

although I did not spent time on the setter body... I suppose the question was more about the metprogramming technic, and that you don't want a pre-mashed solution ;)

By the way...an other solution is to use [opDispatch](https://dlang.org/spec/operatoroverloading.html#dispatch):

```d
class Color {}

class Rectangle {
     private Color fillColor;
     private Color strokeColor;
     private uint strokeWidth;

     auto opDispatch(string member, T)(auto ref T t)
     {
              static if (member == "setStrokeWidth") {}
         else static if (member == "setStrokeColor") {}
         else static if (member == "setFillColor") {}
         else static assert(0, "cannot set " ~ member);
         return this;
     }
}

void main()
{
     (new Rectangle)
         .setStrokeWidth(0)
         .setStrokeColor(null)
         .setFillColor(null);
}
```

Ugh, don't do it that way. Always give opDispatch a template constraint or it will suck to use.

Also, given the problem constraints, you can build the method automatically using the string.

```d
auto opDispatch(string member, T)(auto ref T t) if(member.startsWith("set"))
{
   mixin(toLower(m[3]), m[4 .. $], " = t;");
}
```

-Steve

Reply via email to