Static associative array expressions

Kagamin spam at here.lot
Mon Sep 20 06:11:20 UTC 2021


On Sunday, 19 September 2021 at 11:55:04 UTC, Steven 
Schveighoffer wrote:
> On 9/19/21 1:43 AM, Stefan Koch wrote:
>
>> What do you guys think?
>
> I think an easier fix is just to change the call that does the 
> AA literals to something that can be CTFEd. Right now it's an 
> extern(C) hook that is opaque and takes void[] and TypeInfos.
>
> Something like:
>
> ```d
> V[K] __aALiteral(V, K)(K[] keys, V[] values);
> ```
>
> Then just lower the call to that, and if that is CTFEable, it 
> works.
>
> -Steve

Isn't that already kinda possible?
A quick example:
```d
struct StaticHashTable(TKey,TValue)
{
	struct Pair { TKey key, TValue value }
	Pair[] items;
	inout(TValue) opIndex(in TKey key) pure inout
	{
		foreach(i;items)if(i.key==key)return i.value;
		assert(false);
	}
}

StaticHashTable!(TKey,TValue) make(TKey,TValue)(in TValue[TKey] 
aa) pure
{
	StaticHashTable!(TKey,TValue) s;
	s.items.length=8;
	s.items[0].key="a";
	s.items[0].value=aa["a"];
	return s;
}

immutable s=make(["a":"b","c":"d"]);
static assert(s["a"]=="b");
```
That is, you can serialize a hashtable into anything with a 
relatively easy syntax.


More information about the Digitalmars-d mailing list