在运行时填充不可变对象的数组/关联数组

时间:2015-04-11 11:53:13

标签: immutability d

是否可以在D中在运行时填充不可变对象的数组或关联数组?我有一组不可变的定义,其数据在运行时从外部源读取。每个定义还包含几个其他不可变对象的数组。但是,似乎编译器禁止使用循环实例化不可变数组。在这个例子中(简化了现场数据):

immutable class Definition {
    string name;
    int size;
    immutable(Field)[string] fields;

    immutable class Field {
        int num;
        this (int num) {
            this.num = num;
        }
    }
    this(string name, int size) {
        this.name = name;
        this.size = size;
        foreach (i; 0..size) {
            string key = [cast(char)('a' + i)]; // a, b, c...
            auto field = new immutable(Field)(i);
            fields[key] = field; // ERROR
        }
    }
}
class Instance {
    Definition def;
    void speak() {
        writefln("I am currently referencing definition '%s' (%d)", def.name, def.size);
    }
}

auto defA = new immutable(Definition)("AAA", 4);
auto defB = new immutable(Definition)("BBB", 6);
auto instance = new Instance();
instance.def = cast(Definition) defA;
instance.speak();
instance.def = cast(Definition) defB;
instance.speak();

我正在错误:不可变字段'字段'循环或标签后不允许初始化。有没有办法解决这个问题,或者更好的方法来实现它?或者我是以错误的方式解决这个问题?

1 个答案:

答案 0 :(得分:4)

您需要使用辅助函数和临时函数。

--- test.d  2015-04-11 12:03:54.257670300 +0000
+++ test2.d 2015-04-11 12:03:59.433170300 +0000
@@ -1,3 +1,4 @@
+import std.exception;
 import std.stdio;

 immutable class Definition {
@@ -14,11 +15,13 @@
     this(string name, int size) {
         this.name = name;
         this.size = size;
+        immutable(Field)[string] fields;
         foreach (i; 0..size) {
             string key = [cast(char)('a' + i)]; // a, b, c...
             auto field = new immutable(Field)(i);
-            fields[key] = field; // ERROR
+            add(fields, key, field);
         }
+        this.fields = fields.assumeUnique();
     }
 }
 class Instance {
@@ -38,3 +41,9 @@
    instance.def = cast(Definition) defB;
    instance.speak();
 }
+
+void add(K, V)(V[K] aa, K k, V v)
+{
+   assert(k !in aa);
+   cast()aa[k] = cast()v;
+}