Proposal: clean up semantics of array literals vs string literals

Don Clugston dac at nospam.com
Thu Oct 4 00:40:15 PDT 2012


On 02/10/12 17:14, Andrei Alexandrescu wrote:
> On 10/2/12 7:11 AM, Don Clugston wrote:
>> The problem
>> -----------
>>
>> String literals in D are a little bit magical; they have a trailing \0.
> [snip]
>
> I don't mean to be Debbie Downer on this because I reckon it addresses
> an issue that some have, although I never do. With that warning, a few
> candid opinions follow.
>
> First, I think zero-terminated strings shouldn't be needed frequently
> enough in D code to make this necessary.

[snip]

You're missing the point, a bit. The zero-terminator is only one symptom 
of the underlying problem: string literals and array literals have the 
same type but different semantics.
The other symptoms are:
* the implicit .dup that happens with array literals, but not string 
literals.
This is a silent performance killer. It's probably the most common 
performance bug we find in our code, and it's completely ungreppable.

* string literals are polysemous with width (c, w, d) but array literals 
are not (they are polysemous with constness).
For example,
"abc" ~ 'ü'
is legal, but
['a', 'b', 'c'] ~ 'ü'
is not.
This has nothing to do with the zero terminator.



More information about the Digitalmars-d mailing list