Struct initializer in UDA

realhet real_het at hotmail.com
Sun Sep 27 10:17:39 UTC 2020


On Saturday, 26 September 2020 at 17:13:17 UTC, Anonymouse wrote:
> On Saturday, 26 September 2020 at 16:05:58 UTC, realhet wrote:
> The closest I can get is @(S.init.c(9).f(42)) with use of 
> opDispatch, which is easier to read but still ugly.

All I can get is that the
- an identifier of a member is stronger than the opDispatch. -> 
Error: function expected before (), not S(0, 0).c of type int
- and if I prefix it with '_' it ruins toString. -> Error: no 
property toString for type onlineapp.S


import std.stdio, std.range, std.algorithm, std.traits, std.meta, 
std.conv, std.string, std.uni, std.meta, std.functional, 
std.exception;

struct S{
     int a, b;

     auto opDispatch(string name, T)(T value)
     if(name.startsWith("_"))
     {
         mixin(name[1..$], "= value;");
         return this;
     }
}

void main(){
     S.init._a(5).writeln;
}


Now I'm more confused, as the compiler completely ignores the 
if(name.startsWith("_")) constraint o.O


More information about the Digitalmars-d-learn mailing list