www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - char[] and wchar[] cannot be used as OutputRange

reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Feb 03 2012
parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
char[] and wchar[] could still define a put method, which would make them 
output ranges.  This is worth a bug report.

"Ali Çehreli" <acehreli yahoo.com> wrote in message 
news:jgh4a1$1286$1 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 

Feb 03 2012