AA with immutable values

Roman D. Boiko rb at d-coding.com
Sun Jun 24 05:23:11 PDT 2012


On Sunday, 24 June 2012 at 12:15:10 UTC, Ali Çehreli wrote:
> Although it sounds like a limitation, it may be due to the fact 
> that AA may need to rehash, which may not be suitable with 
> immutable values.
Thanks, now I can see the motivation.

> The following are two ways of generating immutable AA values 
> that I can think of:
>
> import std.exception;
>
> class SomeClass
> {}
>
> class S
> {}
>
> void main()
> {
>     /* By initialization */
>     immutable(SomeClass)[S] _map = [ S.init : null ];
>
>     /* By a function */
>     immutable(SomeClass[S]) makeMap()
>     {
>         SomeClass[S] result;
>         result[S.init] = null;
>
>         return assumeUnique(result);
>     }
>
>     /* Note that the entire AA is immutable: */
>     auto myImmMap = makeMap();
> }
>
> Ali
Unfortunately, this doesn't fit my use case. I needed this to 
memoize creation of immutable objects. I plan to not use AA in 
the future (it is possible to optimize, since the key is some 
string), so this is not a critical problem in my case. I was just 
wondering.


More information about the Digitalmars-d-learn mailing list