Proposal: clean up semantics of array literals vs string literals

deadalnix deadalnix at gmail.com
Tue Oct 2 04:26:18 PDT 2012


Well the whole mess come from the fact that D conflate C string and D 
string.

The first problem come from the fact that D array are implicitly 
convertible to pointer. So calling D function that expect a char* is 
possible with D string even if it is unsafe and will not work in the 
general case.

The fact that D provide tricks that will make it work in special cases 
is armful as previous discussion have shown (many D programmer assume 
that this will always work because of toy tests they have made, where in 
case it won't and toStringz must be used).

The only sane solution I can think of is to :
  - disallow slice to convert implicitly to pointer. .ptr is made for that.
  - Do not put any trailing 0 in string literal, unless it is specified 
explicitly ( "foobar\0" ).
  - Except if a const(char)* is expected from the string literal. In 
case it becomes a Cstring literal, with a trailing 0. This is made to 
allow uses like printf("foobar");

In other terms, the receiver type is used to decide if the compiler 
generate a string literal or a Cstring literal.

Other addition of 0 are just confusing, and will make incorrect code 
work in special cases, which is something you usually don't want. Code 
that work by accident often backfire in spectacular ways at the least 
expected moment.


More information about the Digitalmars-d mailing list