Construct an used-defined hash table container type from an AA-literal expression

user1234 user1234 at 12.de
Mon Jul 6 01:43:43 UTC 2020


On Sunday, 5 July 2020 at 21:38:12 UTC, Per Nordlöw wrote:
> On Sunday, 5 July 2020 at 21:22:13 UTC, ikod wrote:
>> struct AA(K,V)
>> {
>>     void opAssign(V[K] aa)
>>     {
>>     }
>> }
>>
>> void main()
>> {
>>     AA!(string, int) custom_aa;
>>     custom_aa = ["one":1, "two":2];
>> }
>
> Forget to mention that I want the assign call (to `opAssign`) 
> in `main` to be non-(GC)heap allocating like when using C++'s 
> std::initializer_list.

Hereh we go ;)

---
import std;

struct AA
{
     void opIndexAssign(int v, string k) @nogc
     {}
}

void main(string[] args) @nogc
{
     AA myCustom;

     enum literal = ["one":1, "two":2].stringof[1..$-1];
     enum pairs   = literal.split(',').array;
     static foreach (p; pairs)
     {
         myCustom[mixin(p.split(':')[0])] = mixin(p.split(':')[1]);
     }
}
---


More information about the Digitalmars-d-learn mailing list