-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make it possible to iterate over ListBuilder
#212
Comments
As an extra, it would be great if |
So I guess it would be a new interface holding /// A collection that can expose its contents as an `Iterable`. The only safe way to
/// use it is to immediately copy `asIterable`.
abstract class AsIterable<T> {
Iterable<T> asIterable();
} |
Given that /// A collection that expose its contents as an [Iterable]. If the collection is modifiable,
/// then make sure the collection is not modified while using the content as [Iterable],
/// otherwise copying the content is advisable. |
Is there a particular reason ListBuilder implements Iterable-like methods that behave in intrinsically different ways than those on Iterable itself? It feels like an antipattern to mimic an Iterable while quietly mutating the base object (possibly bug-inducing), especially when it's preventing it from actually implementing Iterable and providing the expected functionality there. |
Yes; builders are all about mutating one instance, mixing methods that create new instances doesn't work. If you look at the examples of using the built collection "rebuild" method, you'll see what I mean.
If We could instead make |
Ah, fascinating. This seems to stem from the fact that the For example: // built_list.dart
BuiltList<E> rebuild(Iterable<E> Function(ListBuilder<E>) updates) =>
(toBuilder()..update(updates)).build();
// list_builder.dart
void update(Iterable<E> Function(ListBuilder<E>) updates) =>
replace(updates(this)); |
If a method mutates state, it's confusing to also return it; that's how other languages do the builder pattern, the "fluent" API style, but Dart can do that for free with the cascade operator. The fundamental thing here is that BuiltList has only non-mutating methods that return new results, and ListBuilder only mutating APIs that return nothing. As part of that it has methods that resemble Iterable methods, but it can't actually be the same. Use of the Iterable-like methods on builders is relatively rare, it's tempting to say they should just have been omitted. But there is an important way of using built collections where you create a builder at the start of a block of code then build it at the end. This needs the more detailed methods so you don't have to keep switching between built lists and builders to get the operation you want. |
Oh, I was suggesting that just within the context of the |
Currently the only way of iterating over a
ListBuilder
isbuild().asList()
. It would be great to have a more friendly way to do it, something like a.asIterable()
method.The text was updated successfully, but these errors were encountered: