Accessors, byLine, input ranges

Michel Fortin michel.fortin at michelf.com
Fri Jan 29 05:03:13 PST 2010


On 2010-01-29 06:25:44 -0500, Pelle Månsson <pelle.mansson at gmail.com> said:

> On 01/29/2010 05:48 AM, Michel Fortin wrote:
>> 
>> No. Calling byLine doesn't change the stream. It returns a different
>> view of stdin, which can be used to modify the stream, or not. I think
>> it should be a property. If it was 'getNextLine' then it should be a
>> function. As a proof, this doesn't have any effect:
>> 
>> stdin.byLine;
> 
> It reads a line off stdin. Try it.

Ah, I see, you're right. Silly me for not trying.

So it's not an accessor after all. The problem is that, even 
disregarding the property syntax, the name strongly suggests it's an 
accessor, but it has side effects, which it shouldn't. Either we change 
the name to something else, like 'consumeLines()', or we make it behave 
like an accessor. I'd go for the second option.

The basic problem lies in the very basic definition of an input range. 
Due to its interface (popFront + front), an input range is forced to 
consume the its first element by its mere existence. I think 
constructing an input range shouldn't have side effects. It should be 
more symmetrical with an output range. It could have just one function, 
'take' to get the next element, and now byLine could work correctly.


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list