www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Best practice for strings across D1 and D2

reply "nedbrek" <nedbrek yahoo.com> writes:
Hello,
   I have some wrappers for C functions which I am exporting to D.

For D1, I can just use char*.

For D2, I get compiler errors when I try to assign string constants to my 
char* variables.  It seems the best way is to make them immutable(char).

1) If I make the functions take immutable(char), will that cause future 
problems when people want to pass in things besides string constants?

2) What is the best way to make the same declarations work for D1 and D2? 
It seems everything inside a "version" statement must parse correctly, and 
D1 doesn't want to parse "immutable(char)"...

Thanks!
Ned
Mar 15 2011
next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Wed, 16 Mar 2011 02:40:32 +0300, nedbrek <nedbrek yahoo.com> wrote:

 Hello,
    I have some wrappers for C functions which I am exporting to D.

 For D1, I can just use char*.

 For D2, I get compiler errors when I try to assign string constants to my
 char* variables.  It seems the best way is to make them immutable(char).

 1) If I make the functions take immutable(char), will that cause future
 problems when people want to pass in things besides string constants?

 2) What is the best way to make the same declarations work for D1 and D2?
 It seems everything inside a "version" statement must parse correctly,  
 and
 D1 doesn't want to parse "immutable(char)"...

 Thanks!
 Ned

There are a 2 solutions to this question: - Const!(char)* worked for me (specialize as char* for D1, const(char)* for D2. Too bad it's not in Phobos (might get added if requested, though). - "void foo(in char* bar);" works, too. Use of "auto" will help a lot. "in" implies const here.
Mar 15 2011
prev sibling parent Trass3r <un known.com> writes:
nedbrek Wrote:
 2) What is the best way to make the same declarations work for D1 and D2? 
 It seems everything inside a "version" statement must parse correctly, and 
 D1 doesn't want to parse "immutable(char)"...

You may also use versioned aliases. Just include the D2 code via a string mixin to circumvent the parsing problem.
Mar 17 2011