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

Ali Çehreli acehreli at yahoo.com
Fri Feb 3 09:08:17 PST 2012


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