Splitter.opSlice(), ranges and const strings

spir denis.spir at gmail.com
Thu Feb 24 01:53:33 PST 2011


On 02/24/2011 08:39 AM, Jonathan M Davis wrote:
> On Wednesday 23 February 2011 22:41:53 Christopher Bergqvist wrote:
>> Hi!
>>
>> I've run into an issue which I don't understand.
>>
>> Boiled down code:
>> import std.regex;
>>
>> void main()
>> {
>>         //string str = "sdf"; // works
>>         //const string str = "sdf"; // doesn't work
>>         immutable str = "sdf"; // doesn't work
>>         auto pat = regex(", *");
>>         auto split = splitter(str, pat);
>> }
>>
>> Error:
>> /Library/Compilers/dmd2/osx/bin/../../src/phobos/std/regex.d(3022):
>> Error: this is not mutable
>>
>> Should splitter() be able to cope with const/immutable ranges?
>>
>> (That's with the latest official v2.052 dmd/phobos distribution for
>> mac. I got the same error before upgrading from the v2.051
>> also).
>
> Pretty much _nothing_ copes with const or immutable ranges. And if you think
> about it, it generally makes sense. You can't pop the front off of a const or
> immutable range. So, how could you possibly process it? The are some cases where
> having tail const with ranges would work (assuming that we could have tail const
> with ranges - which we currently can't), but on the whole, const and immutable
> ranges don't really make sense. They can hold const or immutable data, but a
> const or immutable range is pretty useless on the whole.

That's one question I'm wondering about for months (but always forget to ask):
Why should /collection/ traversal shrink them? Why does the regular range 
stepping func (popFront) read, for arrays:
	this.data = thid.data[1..$];
instead of:
	++ this.cursor;
??? Should then be caled eg "stepFront", which imo is much better to express 
the semantics of traversal / iteration.

I guess the issue expressed in this thread is /invented/ by the regular process 
of range, precisely by popFront. There is no reason to mutate a collection just 
to traverse it!

And then, how do you traverse the collection again?

unittest {
     auto a = [1,2,3];
     while (! a.empty()) {
         write(a.front() ,' ');
         a.popFront();
     }
     writeln();

     // below nothing written onto terminal
     while (! a.empty()) {
         write(a.front() ,' ');
         a.popFront();
     }
     writeln();
}

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list