Poll: Primary D version

Mike Parker aldacron at gmail.com
Sat May 22 22:16:50 PDT 2010


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.


More information about the Digitalmars-d mailing list