2 Oct 2012 16:55
zero-terminated strings, string literals, etc
Regan Heath <regan <at> netmail.co.nz>
2012-10-02 14:55:07 GMT
2012-10-02 14:55:07 GMT
Recent discussions on the zero terminated string problems and inconsistency of string literals has me, again, wondering why D doesn't have a 'type' to represent C's zero terminated strings. It seems to me that having a type, and typing C functions with it would solve a lot of problems. The compiler could/would then error if people attempted to pass a D string without converting it correctly. The compiler would create literals with or without \0 as required by the 'type' being assigned, parameter passed, etc. The conversion function from a D string to a C string would return the new type. A %sz format specifier could be added to writef which would be able to type check the argument. As the only way to get a variable of the new type would be from a literal, conversion or C function call so we could be sure it was in fact \0 terminated(*), and so.. An implicit conversion between a C string and a D string (slice using strlen) would be possible, and safe. (Though, not at zero runtime cost) Existing (correct) code would continue to compile, by this I mean: - passing literals - calling a conversion function for each D string argument But code which passes D string variables to C functions without conversion(Continue reading)
RSS Feed