typeof(string.front) should be char

Ali Çehreli acehreli at yahoo.com
Sat Mar 3 06:18:51 PST 2012


On 03/03/2012 05:57 AM, Piotr Szturmaj wrote:
 > Consider a custom range of
 > char:
 >
 > struct CharRange
 > {
 > @property bool empty();
 > @property char front();
 > void popFront();
 > }
 >
 > typeof(CharRange.front) and ElementType!CharRange both return _char_

Yes, and I would expect both to the same type.

 > while for string they return _dchar_. This discrepancy pushes the range
 > writer to handle special string cases.

Yes, Phobos faces the same issues.

 > I'm currently trying to write
 > ByDchar range:
 >
 > template ByDchar(R)
 > if (isInputRange!R && isSomeChar!(ElementType!R))
 > {
 > alias ElementType!R E;
 > static if (is(E == dchar))
 > alias R ByDchar;
 > else static if (is(E == char))
 > {
 > struct ByDchar
 > {
 > ...
 > }
 > }
 > else static if (is(E == wchar))
 > {
 > ...
 > }
 > }
 >
 > The problem with that range is when it takes a string type, it aliases
 > this type with itself, because ElementType!R yields dchar. This is why
 > I'm talking about "bad consequences", I just want to iterate string by
 > _char_, not _dchar_.

In case you don't know already, there are std.traits.isNarrowString, 
std.range.ForEachType, etc. which may be useful.

Ali



More information about the Digitalmars-d-learn mailing list