char[] and wchar[] cannot be used as OutputRange

Daniel Murphy yebblies at nospamgmail.com
Fri Feb 3 09:13:51 PST 2012


char[] and wchar[] could still define a put method, which would make them 
output ranges.  This is worth a bug report.

"Ali Çehreli" <acehreli at yahoo.com> wrote in message 
news:jgh4a1$1286$1 at digitalmars.com...
> This I knew: Being UTF-8 and UTF-16 encodings, and because those encodings 
> are variable-width, char[] and wchar[] cannot be RandomAccessRange ranges 
> (dchar[] can be):
>
> import std.range;
>
> void main()
> {
>     assert(!isRandomAccessRange!( char[]));
>     assert(!isRandomAccessRange!(wchar[]));
>     assert( isRandomAccessRange!(dchar[]));
> }
>
> What I've recently discovered is that for being variable-width encodings, 
> char[] and wchar[] cannot be used as OutputRange ranges either. This is 
> because strings are ranges of Unicode characters in D, so their .front 
> must return a dchar, and that dchar must be an rvalue in the cases of 
> char[] and wchar[], which cannot be assigned to.
>
> Only dchar[] has assignable elements:
>
> import std.range;
>
> void main()
> {
>     assert(!hasAssignableElements!( char[]));
>     assert(!hasAssignableElements!(wchar[]));
>     assert( hasAssignableElements!(dchar[]));
> }
>
> For that reason, only dchar[] can be used as OutputRange:
>
> import std.range;
>
> void main()
> {
>     assert(!isOutputRange!( char[],  char));
>     assert(!isOutputRange!( char[], wchar));
>     assert(!isOutputRange!( char[], dchar));
>
>     assert(!isOutputRange!(wchar[],  char));
>     assert(!isOutputRange!(wchar[], wchar));
>     assert(!isOutputRange!(wchar[], dchar));
>
>     assert( isOutputRange!(dchar[],  char));
>     assert( isOutputRange!(dchar[], wchar));
>     assert( isOutputRange!(dchar[], dchar));
> }
>
> Ali 




More information about the Digitalmars-d-learn mailing list