Skip to content

Commit

Permalink
Merge pull request #185 from JakobOvrum/immutable-aa-init
Browse files Browse the repository at this point in the history
Add 'Runtime Initialization of Immutable AAs' section
  • Loading branch information
Alex Rønne Petersen committed Nov 4, 2012
2 parents ad5be08 + ed7cb16 commit bf85703
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion hash-map.dd
Expand Up @@ -167,6 +167,39 @@ struct MyString {
$(D opEquals) and $(D opCmp) are consistent with each other when
the struct/union objects are the same or not.)

$(V2
<h3>Runtime Initialization of Immutable AAs</h3>

$(P Immutable associative arrays are often desirable, but sometimes
initialization must be done at runtime. This can be achieved with
a constructor (static constructor depending on scope),
a buffer associative array and $(D assumeUnique):)
---------
immutable long[string] aa;

static this()
{
import std.exception : assumeUnique;
import std.conv : to;

long[string] temp; // mutable buffer
foreach(i; 0 .. 10)
{
temp[to!string(i)] = i;
}

aa = assumeUnique(temp);
}

unittest
{
assert(aa["1"] == 1);
assert(aa["5"] == 5);
assert(aa["9"] == 9);
}
---------
)

<h3>Properties</h3>

Properties for associative arrays are:
Expand Down Expand Up @@ -364,4 +397,4 @@ Macros:
DOLLAR=$
STD_UNI=<a href="phobos/std_uni.html">std.uni</a>
FOO=
CATEGORY_SPEC=$0
CATEGORY_SPEC=$0

0 comments on commit bf85703

Please sign in to comment.