[Issue 1654] Array concatenation should result in mutable or invariant depending on usage

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed May 28 15:36:56 PDT 2008


http://d.puremagic.com/issues/show_bug.cgi?id=1654





------- Comment #16 from schveiguy at yahoo.com  2008-05-28 17:36 -------
Yes, you are correct.  The second form is more permissive.  I didn't see that
originally.

Concatenating arrays of arrays and arrays of pointers are special cases because
a tail-const version exists.

I think as long as the implicit casting works, so:

const(int[])[] a;
const(int)[][] b = a ~ a;
const(int[])[] c = a ~ a;

all works, then it shouldn't be a problem.  It probably will not break generic
code because if you have a generic function:

f(T)(const(T)[] input) {...}

How does one construct a 'tailconst' verison of T given just T?  I think that
was what I was saying earlier (about a typedef).  You could do an is-expression
to get at the underlying type, but then that is specialized for arrays, and it
is no more convoluted than the current state of affairs.

If you know T is always going to be an array, you can do:

f(T)(const(T[])[] input) {...}

and you can universally deal with all types of arrays.

So now, I think the rules are:

If concatenating two arrays together, of type T[]:

If T is of the form const(U[]), then one can assign the result to a type of
const(U)[][]

If T is of the form const(U*), then one can assign the result to a type of
const(U)*[]

If T is of the form const(U), and const(U) implicitly casts to U, then one can
assign the result to a type of U[].

For the above rules, if T is invariant(U) the same style rules apply.

In all cases, one can assign to the result type of T[].

If concatenating two arrays together where the element type of the array varies
only in constancy, and const(T) and invariant(T) implicitly cast to/from T,
then the result can be assigned to invariant(T)[], const(T)[] or T[].

Did I miss anything?  Boy this is getting more complex to explain :)


-- 



More information about the Digitalmars-d-bugs mailing list