On Thursday, 15 March 2018 at 13:18:38 UTC, Simen Kjærås wrote:
On Thursday, 15 March 2018 at 12:00:08 UTC, Robert-D wrote:
I want the function to create a mutable copy from a const or a imutable

Like this:

void main() {
    const S s = S(["": ""]);
    S b = s.dup();
}

How can i do that?

In that case, the problem is that you also have to .dup the aa:

    S dup() const pure {
        return S(aa.dup);
    }

However, it seems aa.dup returns the wrong type - one would expect V[K] for inout(V[K]), but it returns inout(V)[K], or inout(string)[string], in your case. That's apparently a known bug: https://issues.dlang.org/show_bug.cgi?id=14148.

The solution for now, then, is this:

    S dup() const pure {
        return S(cast(string[string])aa.dup);
    }

--
  Simen

Why something like this doesn't compile (with or without the cast on bb.dup)?

struct S {
    string[string] aa;

    S dup() inout pure {
        return S(cast(string[string]) aa.dup);
    }
}

struct SS {
    S[] bb;

    SS dup() inout pure {
        return SS(cast(S[]) bb.dup);
    }
}


Error: static assert: "Cannot implicitly convert type inout(S) to S in dup."

Or:

const(S)[] ss = [S(["": ""])];
S[] d = ss.dup;

Error: template object.dup cannot deduce function from argument types !()(const(S)[]), candidates are: /dlang/dmd/linux/bin64/../../src/druntime/import/object.d(2086): object.dup(T : V[K], K, V)(T aa) /dlang/dmd/linux/bin64/../../src/druntime/import/object.d(2122): object.dup(T : V[K], K, V)(T* aa) /dlang/dmd/linux/bin64/../../src/druntime/import/object.d(4191): object.dup(T)(T[] a) if (!is(const(T) : T)) /dlang/dmd/linux/bin64/../../src/druntime/import/object.d(4207): object.dup(T)(const(T)[] a) if (is(const(T) : T))


Reply via email to