Construct an used-defined hash table container type from an AA-literal expression
Atwork
fwafwa at fwfafa.com
Mon Jul 6 11:55:58 UTC 2020
On Monday, 6 July 2020 at 11:51:19 UTC, Jacob Carlborg wrote:
> On Monday, 6 July 2020 at 01:43:43 UTC, user1234 wrote:
>
>> 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;
>> ---
>
> That split won't work if you have something more complicated,
> like struct values:
>
> struct Foo
> {
> int a;
> int b;
> }
>
> enum literal = ["one":Foo(1, 2), "two":Foo(3,
> 4)].stringof[1..$-1];
> enum pairs = literal.split(',').array;
> static assert(pairs == 4);
>
> --
> /Jacob Carlborg
Wouldn't work if the keys had , in them either. So don't even
need a struct to break it.
enum literal = ["one,two": 12, "two,three": 23].stringof[1..$-1];
// Now split(",") won't work either.
More information about the Digitalmars-d-learn
mailing list