Casting between char[]/wchar[]/dchar[]

Hasan Aljudy hasan.aljudy at gmail.com
Sat Aug 5 13:16:05 PDT 2006



Walter Bright wrote:
> kris wrote:
> 
>> Hasan Aljudy wrote:
>>
>>> What are the rules for implicit/explicit casting between char[] and 
>>> wchar[] and dchar[] ?
>>>
>>> When one casts (explicitly or implicitly) does the compiler 
>>> automatically invoke std.utf.toUTF*()?
>>>
>>> Here's an idea that should simplify much of string handling in D:
>>> allow char[] and wchar[] and dchar[] to be castable implicitly to 
>>> each other, provided that the compiler invokes the appropriate 
>>> std.utf.toUTF* method.
>>> I think this is perfectly safe; no data is lost, and string handling 
>>> can become much more flexable.
>>>
>>> Instead of writing three version of the same funciton for each of 
>>> char[] wchar[] and dchar[], one can just write a wchar[] version (for 
>>> example) and the compiler will handle the conversion from/to char[] 
>>> and dchar[].
>>>
>>> This is also relevies developers from writing templetized 
>>> functions/class when they deal with strings.
>>>
>>> Thoughts?
>>
>>
>> This one was beaten soundly around the head & shoulders in the past :)
>>
>> In a systems language like D, one could argue that hidden conversions 
>> and/or translations (a) can mask what would otherwise be unintended 
>> compile-time errors (b) can be terribly detrimental to performance 
>> where multiple conversions are implicitly applied. Such an environment 
>> could potentially put C0W to shame in terms of heap abuse -- recall 
>> some of the recent CoW examples, and sprinkle in a few unintended 
>> conversions for good measure :)
>>
>> IIRC, the last time this came up there was a pretty strong feeling 
>> that such things should be explicit (partly because it can be an 
>> expensive operation ~ likely sucking on the heap also).
> 
> 
> Yes. It's hard to judge where the line is, but too many implicit 
> conversions leads to very hard to understand/debug programs.

Can I ask you atleast to simplify the conversion by adding properties 
utf* to char/wchar/dchar arrays?

so, if I have:
----
char[] process( char[] str ) { ... }

...

dchar[] my32str = .....;

//I can write
my32str = process( my32str.utf8 ).utf32;

//instead of
//my32str = toUTF32( process( toUTF8( my32str ) ) );
----





More information about the Digitalmars-d mailing list