www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - RandomAccessRange / Mobile Elements

reply "Johann" <johann qw.erty> writes:
Hello,
I read in std.range that given a random access range r , 
r.opIndex(n) should return a reference to the nth element. Is 
there a qualifier for a "read only" random access range? If not, 
why?
I also don't really get the point of "Mobile Elements", how is 
"destructively reading" related to mobility, and what is it good 
for?

Thanks.
May 27 2014
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Tuesday, 27 May 2014 at 13:04:50 UTC, Johann wrote:
 Hello,
 I read in std.range that given a random access range r , 
 r.opIndex(n) should return a reference to the nth element. Is 
 there a qualifier for a "read only" random access range? If 
 not, why?
The documentation is a bit wrong. It doesn't actually have to be a reference. You may return by value if you want, which is a "form" of read-only access. If you want to return actual const references or elements, you may do so if you so wish, but this would done via the range's type itself, rather than the function. Eg, you'd have "Range" and "ConstRange". It's currently a big subject of discussion.
 I also don't really get the point of "Mobile Elements", how is 
 "destructively reading" related to mobility
If you move something from A to B, then whatever was at A is not there anymore. How is that not destructively reading?
 and what is it good for?
Arguably, performance. If you don't need to re-use your elements at a future date, then instead of creating a duplicate copy via postblit (which could be arbitrarily expensive), we simply move the data, which is much cheaper.
 Thanks.
You're welcome :)
May 27 2014