On 03/13/2010 02:13 PM, Walter Bright wrote:
Currently, given an array:

alias T[3] A;
A a;

the default initializer for A, A.init, is T.init. It is done this way
for memory efficiency, as:

a = A.init;

doesn't need to create an array for the rvalue. But it does cause
generic programming problems, especially with the advent of static
arrays now being passed by value rather than by ref.

So, I propose changing A.init from being T.init to being [T.init,
T.init, T.init].

What do you think?

The irregularity of .init for arrays has been a huge source of problems. I'm very glad you're looking into fixing it.

My understanding is that you propose to transform A.init into a literal with as many elements as the length of the array. That has a an issue. Currently array literals default to T[], not T[N]. So this would do the unexpected:

alias int[3] A;
A a;
auto b =  A.init;
assert(is(typeof(b) == int[]); // pass

I was thinking of rewriting (T[N]).init as:

{ T[N] result; return result; }()

i.e. an rvalue of type T[N]. The lambda should be CTFE-able and does not occupy static storage because it creates the temporary on the stack (well, for large arrays the compiler would be free to resort to dynamic allocation).

Your solution works if you rewrite (T[N]).init as:

(cast(T[N]) [ T.init, T.init, T.init, ... ])


Andrei

Reply via email to