subspace icon indicating copy to clipboard operation
subspace copied to clipboard

Iterator extension point

Open danakj opened this issue 1 year ago • 0 comments

With Rust traits, it's simple to add methods to a type (or all types that satisfy a trait!) with a trait, so the itertools crate can add a bunch of more niche or powerful functions on Iterator types. We don't have this luxury in C++ but it's important that a library, or application, can add more methods to iterators.

We have one extension point now, which is generate() and allows you to insert a generator function that does anything you like. But it would be very nice to be able to add methods to Iterators too. So here's the idea:

IteratorBase::extend() or.. ::mixin() (I hate that word for some reason?) or something in some shade of colour. This method takes a type parameter of T and returns an Iterator type that subclasses IteratorBase (as final) and T. Now T can provide iteratory methods beyond what IteratorBase provides.

Let's verify this works the way it seems like it should work. That T is a bit tricky to implement cuz concepts/templates are tricky to implement. It needs to work much like IteratorBase does, and will need to use the CRTP to know the actual final iterator type in order to cast to it and call next(), or produce chained iterators with the final iterator type (or SizedIterator of it) in them. At least there's lots of examples in IteratorBase though.

danakj avatar Jul 09 '23 17:07 danakj