On 10/10/2009 10:11, Don wrote:
Christopher Wright wrote:
Don wrote:
I don't understand why runtime-determined array literals even exist.
They're not literals!!!
They cause no end of trouble. IMHO we'd be *much* better off without
them.

You don't see the use. I do. I would go on a murderous rampage if that
feature were removed from the language.

For example, one thing I recently wrote involved creating a process
with a large number of arguments. The invocation looked like:
exec("description", [procName, arg1, arg2] ~ generatedArgs ~ [arg3,
arg4] ~ moreGeneratedArgs);

There were about ten or fifteen lines like that.

You'd suggest I rewrite that how?
char[][] args;
args ~= procName;
args ~= arg1;
args ~= arg2;
args ~= generatedArgs;
args ~= arg3;

Of course not. These runtime 'array literals' are just syntax sugar for
a constructor call. Really, they are nothing more.
At worst, it would be something like:

exec("description", createArray(procName, arg1, arg2) ~ generatedArgs ~
createArray(arg3, arg4) ~ moreGeneratedArgs);

Depending on what the 'exec' signature is, it could be simpler than
that. But that's the absolute worst case.

The language pays a heavy price for that little bit of syntax sugar.


You keep calling these literals "constructor calls' and I agree that that's what they are. My question is then why not make them real constructors?

auto a = new int[](x, y, z);
auto b = new int[3](x, y, z);
auto c = new int[]; // empty array
auto d = new int[3]; // use default ctor: d == [0,0,0]

for arrays of class instances this could be extended to call a constructor for each index, something like:

// tuples would be very handy here..
auto e = new Class[2](Tuple!(args1), Tuple!(args2));

Reply via email to