Why does `filterBidirectional` exist, and why isn't it called `filter`?

Steven Schveighoffer schveiguy at gmail.com
Thu Mar 9 19:48:21 UTC 2023


On 3/9/23 12:07 PM, Paul Backus wrote:
> On Thursday, 9 March 2023 at 13:16:07 UTC, Steven Schveighoffer wrote:
>> This has been a constant debate -- should ranges be allowed to lazily 
>> initialize on the first call to front (or back)?
>>
>> I'd say no. back/front shouldn't be modifying operations. I dislike 
>> the idea of storing a "was initialized" flag and having to check that 
>> on every call.
>>
>> That being said, there's no formal requirement for it. It's just a 
>> convention I think Phobos should stick to for its included ranges.
> 
> I think probably you have to go on a case-by-case basis. `File.byLine`, 
> for example, does not and should not precompute `front`, because doing 
> so is potentially very expensive. OTOH, `iota` does and should 
> precompute `front`, because doing so is essentially free and makes the 
> implementation much simpler.

I disagree. If you construct a `byLine` you intend to iterate by lines. 
Fetching the first line is reasonable and expected (by me anyway).

The big issue with `byLine`'s iteration mechanism is whether `popFront` 
should consume the next line or not. But that is a semantic expectation 
that people can differ on.

Again, as I said elsewhere, using wrappers to compose the desired 
behavior should be what we use, and all ranges should just always have 
the same expected semantics.

-Steve


More information about the Digitalmars-d mailing list