Struct initializers and const in 2.009

Christopher Wright dhasenan at gmail.com
Mon Jan 7 05:51:50 PST 2008


torhu wrote:
> Dan wrote:
>> You actually managed to make your programs compatible with D 1.x and 
>> 2.x !?!?
> 
> Only some bindings to a C library, so no classes or const methods 
> necessary.
> 
>> I spent a good 40 minutes on my source, and ultimately realized it's 
>> impossible for me to do so;
>>
>> version(D_Version2)
>>   alias const(char)[] const_string;
>> else
>>   alias char[] const_string;
>>
>> Value {
>>   const Value opIndex(const_string){
>>     bla bla   }
>> }
>>
>> You can't alias out the const declaration for the function, and you 
>> can't get rid of it in 2.x and still have the program work, let alone 
>> have the desired functionality.
> 
> Maybe you could make it work with some casting, but it might not be 
> worth it.  The const_string alias will work if you use a string mixin:
> 
> version(D_Version2)
>    mixin("alias const(char)[] const_string;");
>  else
>    alias char[] const_string;
> 
> But are you sure you need this, won't the Phobos string alias work? It's 
> defined in object.d.

And then you can just do:
static if (!is(typeof(string))) alias char[] string;

Future-proofs in case your code works with Tango and it gets a string 
alias in the future.



More information about the Digitalmars-d mailing list