I can't figure out how to copy an associative array from inside a pure function.

import std.stdio;
import std.variant;

void main() {
immutable Variant[string] nested_map = ["This":mv(["is":mv(),"a":mv()]),
                                                                  
"nested":mv(["map":mv()])];
        Variant[string] res_map = nested_map.deep_dup;
        writeln(res_map);
}

@safe
pure Variant[K] deep_dup(K)(const Variant[K] map) {
        Variant[K] res;
        foreach(const K key, const Variant value; map) {
                const Variant[K] under_value = value.get!(const Variant[K]);
                res[key] = under_value.deep_dup;
        }
        return res;
}

@safe
pure Variant mv() {
        Variant[string] inner;
        return Variant(inner);
}

@safe
pure Variant mv(T)(T obj) {
        return Variant(obj);
}

generates a compiler error:

source/app.d(15): Error: pure function 'app.deep_dup!string.deep_dup.__foreachbody2' cannot call impure function 'std.variant.VariantN!(24u).VariantN.get!(const(VariantN!(24u)[string])).get' source/app.d(16): Error: pure nested function '__foreachbody2' cannot access mutable data 'res' source/app.d(16): Error: pure nested function '__foreachbody2' cannot access mutable data 'res' source/app.d(16): Error: pure nested function '__foreachbody2' cannot access mutable data 'res' source/app.d(7): Error: template instance app.deep_dup!string error instantiating

Reply via email to