Compile-time global mutables

HuskyNator HuskyNator at protonmail.ch
Thu Jun 30 13:04:39 UTC 2022


On Thursday, 30 June 2022 at 12:55:43 UTC, bauss wrote:
> You can already do that.
>
> ```d
> enum counter(size_t x = [__traits(allMembers, 
> mixin(__MODULE__))].length)=x;
> char[] member(ulong x)
> {
>     char[] buf = "void[0] _X0000000000000000;".dup;
>     enum mapping = "0123456789abcdef";
>     foreach_reverse(i; buf.length-17 .. buf.length-1)
>     {
>         buf[i] = mapping[x & 0xf];
>         x >>= 4;
>     }
>     return buf;
> }
> mixin template increment()
> {
>     mixin(member(counter!()));
> }
>
> pragma(msg, counter!());
> mixin increment;
> pragma(msg, counter!());
> mixin increment;
> pragma(msg, counter!());
> ```
>
> Credits: p0nce

I'm assuming this works for counters in general? (although I do 
not see how the member function works, this seems like a very 
sophisticated / fault-sensitive workaround)
Do you know there's workarounds for the other case(s) I have 
described?


More information about the Digitalmars-d mailing list