Add iteration capability to SegmentingContainer
#20
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an experimental attempt to make
SegmentingContainer
iterable.Problems
The main problem that this presents can be described in the following way:
ContainerInterface
is given, how can a decorator iterate over its members? Spoiler: it cannot.ContainerInterface
is alsoTraversable
(such asMapInterface
), how does one switch determine what the next element is, since the inner container will enumerate all members and not only those that begin with the root key?Traversable
, how does one indicate that for the return value ofget()
in a type-safe way?SegmentingIterator#get()
returns copies of itself. And itself is either traversable or not, which means that all retrieved segments are either traversable or not. For proper typing, some would need to be traversable, and some not - depending on whether the inner container has multiple items that start with the root key.More Problems
This also opens questions like these:
How does one separate the capabilities of key retrieval and of iteration, such that the latter can extend the former?
In general, how does one extend functionality of a type in a maintainable way, if the type it is extending limits information about the value it wraps - information that is critical to the extension?
Example. A
SegmentingContainer
allows reading information by a specific key only. ASegmentingMap
(an iterable segmenting container) would need to iterate over the data in theSegmentingContainer
. But it canot do that, because it is agnostic of the actual data storage, and the container it wraps does not enumerate its members - in fact, they may not be enumerable at all.Inheritance and compile-time composition are also available options. However, this would reduce maintainability, and add complexity.