Poll: Primary D version

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun May 23 06:21:32 PDT 2010

On 05/23/2010 12:16 AM, Mike Parker wrote:
> Walter Bright wrote:
>> Robert Clipsham wrote:
>>> On 22/05/10 17:42, Andrei Alexandrescu wrote:
>>>>> - Interfacing to C libraries is now overly complex thanks to const
>>>>> correctness. After updating all the function signatures I found phobos
>>>>> was completely lacking the functions to convert between C and D
>>>>> strings
>>>>> of varying constness or with different encodings
>>>>> (char/wchar/dchar).. I
>>>>> ended up writing my own functions
>>>> Could you please give more detail on that? There should be essentially
>>>> no problem with using C-style strings with D regardless of constness.
>>> extern(C)void someFunc(char*);
>>> There is no function in phobos which will allow me to call this
>>> function using a D string, toStringz() gives:
>>> test.d(4): Error: function test.someFunc (char*) is not callable
>>> using argument types (const(char)*)
>>> Unless I cast away const, which isn't pretty if you've got a lot of
>>> these functions, unless you write a wrapper for each one (my current
>>> hack). to!() doesn't support it at all, and I can't find another
>>> method in phobos for it.
>> What's necessary is to decide if someFunc changes the string data or
>> not. If it does not, then it should be prototyped as:
>> extern (C) void someFunc(const char *);
>> If it does, then the char* is the correct declaration, and an
>> immutable string should not be passed to it.
> That's not the problem. The problem is this:
> const(char)* toStringz(const(char)[] s);
> There's no equivalent for:
> char *toStringz(char[] s);
> Hence the need to cast away const or use a wrapper for non-const char*
> args.

Yah, and that's intentional. APIs that use zero-terminated strings for 
output are very rare and most often inherently unsafe.


More information about the Digitalmars-d mailing list