string literal error?

Simen Kjaeraas simen.kjaras at gmail.com
Thu May 8 11:57:13 PDT 2008


On Thu, 08 May 2008 20:37:17 +0200, badmadevil <badmadevil at gmail.com>  
wrote:

> badmadevil wrote:
>> Hi, sorry if already discussed.
>>  codes:
>>   string x = "bug?" ;
>>   string y = "bug?" ;
>>   string z = "bug?".reverse ;
>>   writefln("%s : %s : %s", x, y, z) ;
>>   x.reverse ;
>
> same effect if x is replace with literal "bug?", ie.
>    "bug?".reverse ;
>
>>   writefln("%s : %s : %s", x, y, z) ;
>>  output - D 1.027 & 2.013:
>>  >?gub : ?gub : ?gub
>>  >bug? : bug? : bug?
>>  Should x, y, z be independent to each others?

This happens because .reverse does in-place reversal. The
correct way to do it would be:

   string x = "bug?".idup;
   string y = "bug?".idup;
   string z = "bug?".idup.reverse;
   writefln(x,y,z);
   x.reverse;
   writefln(x,y,z);
   writefln(typeof("bug?").stringof);

Now, seeing as the array contents are invariant in this example,
.reverse is not following the D rules of conduct. (i.e. it's buggy)

-- Simen


More information about the Digitalmars-d-learn mailing list