Using arrays with functions taking ranges

monarch_dodra monarchdodra at gmail.com
Fri Dec 14 07:05:18 PST 2012


On Friday, 14 December 2012 at 14:56:45 UTC, Mu wrote:
>> It "works" because in theory, all mutable ranges verify the 
>> "is output range" trait. However, they are not "sinks", so if 
>> you write too much into them, you'll get an out of index 
>> exception. Does it work at runtime, and do you get the correct 
>> behavior?
>
> From what I tested, yes it works correctly, but I don't 
> understand why.
> If put() is used, and the opSlice has a length different from 
> zero, how come the data is filled in starting at opSlice's 
> first element?
>
> If my questions are becoming trivial, please point me to the 
> relevant documentation.
> Thank you.

No, the question isn't trivial at all.

It works because "put" is defined for all input ranges as "write 
to first element and popFront".

Basically, in C++ terms, it's the same as "*(it++) = value".

The "problem" in this case is that you have to make sure *before 
hand*, that there is enough room to do this. If you were to 
"accidently" stuff into your input range more than it can take, 
you'll error out. EG. the same as going past it_end.

I don't have the context to your program, so I can't give you a 
perfect answer. It sounds like you are doing the right thing.

--------
Long story short:
*Input range: It is pre-allocated, and you can only put stuff up 
to its capacity.
*(appender-style) Output range: Has only "put", and grows as you 
stuff it.


More information about the Digitalmars-d-learn mailing list