What's left to do for a stable D2?

bearophile bearophileHUGS at lycos.com
Fri Jan 22 12:27:02 PST 2010


Thank you for your answers Simen kjaeraas.

>No need for them when we have this: S s = S( 4 );<

If you have to initialize an array of many structs you have to repeat the name of the struct many times, this is redundant, and takes more space (and if you don't use an IDE it needs more time to type):

struct Vector2 { double x, y; }

Vector2[] data1 = [{1,2}, {3,4}, {0,1}, {1,2}, {2,3}, {3,4}, {4,5}, {5,6}, {6,7},
                   {7,8}, {8,9}, {9,10}, {10,11}, {11,12}, {12,13}, {13,14},
                   {14,15}, {15,16}, {16,17}, {17,18}, {18,19}, {19,20}];

Vector2[] data2 = [Vector2(0,1), Vector2(1,2), Vector2(2,3), Vector2(3,4),
                   Vector2(4,5), Vector2(5,6), Vector2(6,7), Vector2(7,8),
                   Vector2(8,9), Vector2(9,10), Vector2(10,11), Vector2(11,12),
                   Vector2(12,13), Vector2(13,14), Vector2(14,15), Vector2(15,16),
                   Vector2(16,17), Vector2(17,18), Vector2(18,19), Vector2(19,20)];

void main() {}


>Literals are allocated in the static data segment, and thus should be immutable.<

In Python I am used to modify the contents of array (list) literals, for example I can initialize them to some values and then change them. So I'd like array literals to be mutable, but I can live with them being immutable and dup them where necessary.

There are times in D code where I write:

foreach (direction; [[-1,+0],[+0,-1],[+1,+0],[+0,+1]]) { ...

Where in a similar situation in Python I write (this bit of pattern matching is very handy, but D devs seems not interested in this):

for sx, sy in [[-1,+0],[+0,-1],[+1,+0],[+0,+1]]: ...

Currently ldc compiles that line of D1 code badly (creating a new array of all the directions in each loop cycle, ugly), with immutable literals there is never a risk of doing:

foreach (ref direction; [[-1,+0],[+0,-1],[+1,+0],[+0,+1]]) { ...

so the array initialization can surely be moved out of the loop (another small problem is that in D2 those little inner length-2 arrays are dynamic arrays, that's not much efficient).

Bye,
bearophile



More information about the Digitalmars-d mailing list