March 22, 2012
On Thu, Mar 22, 2012 at 07:55:09PM +0100, Andrej Mitrovic wrote: [...]
> Hmm, what about (temporary) compatibility with hash literals? You could add the following to the newAA module:
> 
> static import std.traits;
> template KeyType(V : V[K], K)
> {
>     alias K KeyType;
> }
> 
> template ValueType(V : V[K], K)
> {
>     alias V ValueType;
> }

Done, pushed to github.


> and in the AssociativeArray struct:
> void opAssign(AA)(AA aa)
>     if (std.traits.isAssociativeArray!AA
>         && is(KeyType!AA == keytype)
>         && is(ValueType!AA == valuetype))
> {
>     foreach (key, val; aa)
>         this[key] = val;
> }

Done, though this required another opAssign() to replace the default one that got suppressed by this template. Which required some nasty const casts to work properly, but at least D lets you do that when you need to. :-)


> Then this will work:
> 
> AA!(int, string) intToStr;
> intToStr = [1:"foo"];
> assert(intToStr[1] == "foo");
> 
> Otherwise it will be pretty hard to test the new hashes without changing a significant amount of code.

True. I still can't get this to work though:

	AA!(string,int) aa = ["abc":123];

Maybe this needs a ctor?


T

-- 
Almost all proofs have bugs, but almost all theorems are true. -- Paul Pedersen
March 22, 2012
On 3/22/12, H. S. Teoh <hsteoh@quickfur.ath.cx> wrote:
> Maybe this needs a ctor?

Yeah probably. I really don't know when opAssign or the ctor are called (especially with more complex types with alias this). Maybe someone knows this or it's in the spec but I haven't read about it.