tcbrindle / flux

A C++20 library for sequence-orientated programming
https://tristanbrindle.com/flux/
Boost Software License 1.0
513 stars 30 forks source link

Allow sequence impls to be nested classes #14

Closed tcbrindle closed 1 year ago

tcbrindle commented 1 year ago

Previously (ignoring "simple sequences"), there were two ways of implementing the Sequence protocol:

The first case is necessary in order to add Sequence conformance to types we don't own and cannot modify. For types do control though, it's a bit of a pain, as the sequence implementation is separate from (and possibly quite distant from) the type to which it pertains.

The second approach of providing member functions is fine for tiny examples, but (at least until we get deducing this) isn't very useful for more complex sequences as we'd have to duplicate everything for const- and non-const fns; and it doesn't interact all that well with inheriting from lens_base, as we can end up hiding names that we would then need to bring back into scope.

This PR replaces the member function approach by instead allowing you to provide a nested class named MyType::flux_sequence_iface which has the same format as sequence_iface<MyType> -- that is, static methods and nested typedefs etc.

The idea is that this is the best of both worlds: just one way of writing things, but with two options of where to put it (either in-class, or separately).

This has the slight down-side that trivial examples become slightly more verbose, but I don't think any real-world code would actually suffer.

codecov[bot] commented 1 year ago

Codecov Report

Base: 96.68% // Head: 96.94% // Increases project coverage by +0.26% :tada:

Coverage data is based on head (ea65bf7) compared to base (e51a988). Patch coverage: 97.77% of modified lines in pull request are covered.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #14 +/- ## ========================================== + Coverage 96.68% 96.94% +0.26% ========================================== Files 46 45 -1 Lines 1358 1344 -14 ========================================== - Hits 1313 1303 -10 + Misses 45 41 -4 ``` | [Impacted Files](https://codecov.io/gh/tcbrindle/libflux/pull/14?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tristan+Brindle) | Coverage Δ | | |---|---|---| | [include/flux/op/ref.hpp](https://codecov.io/gh/tcbrindle/libflux/pull/14/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tristan+Brindle#diff-aW5jbHVkZS9mbHV4L29wL3JlZi5ocHA=) | `100.00% <ø> (ø)` | | | [include/flux/op/slice.hpp](https://codecov.io/gh/tcbrindle/libflux/pull/14/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tristan+Brindle#diff-aW5jbHVkZS9mbHV4L29wL3NsaWNlLmhwcA==) | `100.00% <ø> (ø)` | | | [include/flux/op/take\_while.hpp](https://codecov.io/gh/tcbrindle/libflux/pull/14/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tristan+Brindle#diff-aW5jbHVkZS9mbHV4L29wL3Rha2Vfd2hpbGUuaHBw) | `100.00% <ø> (ø)` | | | [include/flux/source/empty.hpp](https://codecov.io/gh/tcbrindle/libflux/pull/14/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tristan+Brindle#diff-aW5jbHVkZS9mbHV4L3NvdXJjZS9lbXB0eS5ocHA=) | `86.66% <86.66%> (+41.21%)` | :arrow_up: | | [include/flux/op/bounds\_checked.hpp](https://codecov.io/gh/tcbrindle/libflux/pull/14/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tristan+Brindle#diff-aW5jbHVkZS9mbHV4L29wL2JvdW5kc19jaGVja2VkLmhwcA==) | `100.00% <100.00%> (ø)` | | | [include/flux/op/cache\_last.hpp](https://codecov.io/gh/tcbrindle/libflux/pull/14/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tristan+Brindle#diff-aW5jbHVkZS9mbHV4L29wL2NhY2hlX2xhc3QuaHBw) | `100.00% <100.00%> (ø)` | | | [include/flux/op/cartesian\_product\_with.hpp](https://codecov.io/gh/tcbrindle/libflux/pull/14/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tristan+Brindle#diff-aW5jbHVkZS9mbHV4L29wL2NhcnRlc2lhbl9wcm9kdWN0X3dpdGguaHBw) | `100.00% <100.00%> (ø)` | | | [include/flux/op/drop.hpp](https://codecov.io/gh/tcbrindle/libflux/pull/14/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tristan+Brindle#diff-aW5jbHVkZS9mbHV4L29wL2Ryb3AuaHBw) | `100.00% <100.00%> (ø)` | | | [include/flux/op/drop\_while.hpp](https://codecov.io/gh/tcbrindle/libflux/pull/14/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tristan+Brindle#diff-aW5jbHVkZS9mbHV4L29wL2Ryb3Bfd2hpbGUuaHBw) | `100.00% <100.00%> (ø)` | | | [include/flux/op/filter.hpp](https://codecov.io/gh/tcbrindle/libflux/pull/14/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tristan+Brindle#diff-aW5jbHVkZS9mbHV4L29wL2ZpbHRlci5ocHA=) | `100.00% <100.00%> (ø)` | | | ... and [1 more](https://codecov.io/gh/tcbrindle/libflux/pull/14/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tristan+Brindle) | | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tristan+Brindle). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tristan+Brindle)

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.