Poll: Primary D version

Robert Clipsham robert at octarineparrot.com
Sat May 22 11:26:27 PDT 2010


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.

extern(C)void someFunc(wchar*);

This is impossible with phobos, there's no function to convert a D 
string to wchar*, not even one where I could cast away constness. This 
includes dchar* too.

>> - to!() didn't work in most cases where I tried to use it, I ended up
>> writing my own conversion functions
>
> to is deliberately defined to be restrictive; parse is more forgiving.
> Anyway, I'd be glad to improve to if you gave me a few hints.

Any of the above conversions would be nice, although I appreciate that 
there's no way to tell if it's a C style string or a pointer to a single 
char. There were several other situations which I worked around but 
didn't note down, so I can't list them here.


More information about the Digitalmars-d mailing list