Adapting foreign iterators to D ranges

Steven Schveighoffer schveiguy at gmail.com
Tue Apr 23 02:49:16 UTC 2024


On Monday, 22 April 2024 at 11:36:43 UTC, Chloé wrote:

> The first implementation has the advantage is being simpler and 
> empty being const, but has the downside that next is called 
> even if the range ends up not being used. Is either approach 
> used consistently across the D ecosystem?

I always go for the simplest approach. So that means, pre-fill in 
the constructor.

Yes, the downside is, if you don't use it, then the iterator has 
moved, but the range hasn't. But returning to the iterator after 
using the range is always a dicey proposition anyway.

The huge benefit is that all the functions become simple and 
straightforward.

But there is no "right" approach. And using composition, you may 
be able to achieve all approaches with wrappers. Phobos does 
various things depending on what people thought was good at the 
time. It sometimes causes some very unexpected behavior.

I recommend always using the same approach for the same library, 
that way your users know what to expect!

-Steve


More information about the Digitalmars-d-learn mailing list