Postfix string literal alternative suggestion

Derek Parnell derek at psych.ward
Sat Feb 9 14:04:15 PST 2008


On Sat, 9 Feb 2008 21:28:29 +0000, Janice Caron wrote:

> On 09/02/2008, Walter Bright <newshound1 at digitalmars.com> wrote:
>> You can already use the syntax:
>>
>>         cast(string)"foo";
>>         cast(wstring)"bar";
>>         cast(dstring)"abc";
>>
>> if you need to.
> 
> I did not know that. That seems counterintuitive. I would not expect a
> simple cast to perform UTF conversion. (I would have expected "bar"
> and "abc" both to have type invariant(char)[3]).

The 'cast' form does not do UTF conversion. It only works with literals and
it tells the compiler which format to store the literal in. If you use cast
with variables, no UTF conversion is performed instead it tells the
compiler to pretend that the bits are not what the datatype says.

  dstring ds;
  ...
  cast(char[])ds // Pretend that 'ds' really is a char[].


It is annoying that that sometimes 'cast' means convert and sometimes it
means pretend and sometimes it means something else.

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell



More information about the Digitalmars-d mailing list