Range functions expand char to dchar

Matt Kline via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 8 10:52:11 PDT 2015


After seeing Walter's DConf presentation from this year, I've 
been making an effort to use range algorithms more, such as using 
chain() and joiner() as an alternative to array concatenation and 
std.array.join.

Unfortunately, doing so with strings has been problematic, as 
these algorithms expand strings into dstrings.

An example:

import std.algorithm;
import std.range;
import std.stdio;
import std.regex;

void main()
{
     // One would expect this to be a range of chars
     auto test = chain("foo", "bar", "baz");
     // prints "dchar"
     writeln(typeid(typeof(test.front)));

     auto arr = ["foo", "bar", "baz"];
     auto joined = joiner(arr, ", ");
     // Also "dchar"
     writeln(typeid(typeof(joined.front)));

     // Problems ensue if one assumes the result of joined is a 
char string.
     auto r = regex(joined);
     matchFirst("won't compile", r); // Compiler error
}

Whether by design or by oversight, this is quite undesirable. It 
violates the principle of least astonishment (one wouldn't expect 
joining a bunch of strings would result in a dstring), causing 
issues such as the one shown above. And, if I aim to use UTF-8 
consistently throughout my applications (see 
http://utf8everywhere.org/), what am I to do?


More information about the Digitalmars-d mailing list