VLERange: a range in between BidirectionalRange and RandomAccessRange
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Thu Jan 13 11:08:36 PST 2011
On 1/13/11 8:52 AM, Steven Schveighoffer wrote:
> I see it as having two vast improvements:
>
> 1. If we replace char[] with a specific type for string, then char[] can
> be considered a true array by phobos, and phobos can now deal with a
> char[] array without the need to cast.
> 2. It protects the casual user from incorrectly using a string by making
> the default the correct API.
>
> Those to me are very important.
Let's take a look:
// Incorrect string code
void fun(string s) {
foreach (i; 0 .. s.length) {
writeln("The character in position ", i, " is ", s[i]);
}
}
// Incorrect string_t code
void fun(string_t!char s) {
foreach (i; 0 .. s.codeUnits) {
writeln("The character in position ", i, " is ", s[i]);
}
}
Both functions are incorrect, albeit in different ways. The only
improvement I'm seeing is that the user needs to write codeUnits instead
of length, which may make her think twice. Clearly, however, copiously
incorrect code can be written with the proposed interface because it
tries to hide the reality that underneath a variable-length encoding is
being used, but doesn't hide it completely (albeit for good
efficiency-related reasons).
But wait, there's less. Functions for random-access range throughout
Phobos routinely assume fixed-length encoding, i.e. s[i + 1] lies next
to s[i]. From a cursory look at string_t, std.range will qualify it as a
RandomAccessRange without length. That's an odd beast but does not
change the fixed-length encoding assumption. So you'd need to
special-case algorithms for string_t, just like right now certain
algorithms are specialized for string.
Where's the progress?
Andrei
More information about the Digitalmars-d
mailing list