Array literals' default type

Don nospam at nospam.com
Sat Oct 10 01:11:10 PDT 2009


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.




More information about the Digitalmars-d mailing list