Array literals' default type

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Sat Oct 10 09:12:27 PDT 2009


Christopher Wright wrote:
> 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.
> 
> I'm quite surprised that there is a runtime function for this. I would 
> expect codegen to emit something like:
> 
> array = __d_newarray(nBytes)
> array[0] = exp0
> array[1] = exp1
> ...
> 
>> At worst, it would be something like:
>>
>> exec("description", createArray(procName, arg1, arg2) ~ generatedArgs 
>> ~ createArray(arg3, arg4) ~ moreGeneratedArgs);
> 
> PHP does this. I haven't used PHP enough to hate it.


I've used PHP a fair bit, and I don't hate its array syntax at all. 
(There are plenty of other things in PHP to hate, though.) It's easily 
readable, and not much of a hassle to write. But array() in PHP isn't a 
function, it's a language construct with special syntax. To create an 
AA, for instance, you'd write

   $colours = array("apple" => "red", "pear" => "green");

I'm not sure what the D equivalent of that one should be.

-Lars



More information about the Digitalmars-d mailing list