Comments (7)
It's possible -- I'm not quite clear on how asList is better than toList, though.
The current toList is implemented using a copy-on-write wrapper, so it's cheap to create unless you actually then mutate it. So we have:
toList: fast, when you do a mutate, does a copy the first time
asList: fast, when you do a mutate, throws an exception
If the second is preferable in some cases, sure, we can add it.
FWIW, I try to pass around Iterable where possible. I think this is generally good practice. There are others who believe that Iterable should be used carefully because it might be a lazy iterable ... but for me, it's the people passing around lazy iterables who need to be careful, and the rest of us should use Iterable where possible as it's a nice interface.
Unfortunately there's no equivalent for map. (Iterable of values plus a lookup function is what I sometimes use in practice, but that's pretty horrible.)
from built_collection.dart.
Really this is somewhere I wish we had a good zero-cost abstraction around a mutable list we don't want anyone to modify - perhaps something where asList
could simply be implemented as:
asList() {
var list = _backingList;
assert(() {
list = new List<T>.unmodifiable(list);
});
return list;
}
from built_collection.dart.
Well -- we can do better here.
new List.unmodifiable copies the list, but actually within BuiltList we know the list will not be modified.
So we could return a wrapper that throws on modify, like the current wrapper that copies on modify.
The question is whether the throw behaviour is really a useful thing to have :)
from built_collection.dart.
That's fair.
I think I like having the throw behavior for developers to get failures when they use your API:
external Map getEmployeeMap();
void main() {
// Throws StateError, due to the Map being returned being unmodifiable.
getEmployeeMap().clear();
}
from built_collection.dart.
Hmm. For that example I think not throwing is better, then you can do e.g.:
final map = getEmployeeMap();
map['newEmployee'] = new Employee(); // CopyOnWriteMap does a copy here
store(map);
But maybe there are others where it's weird to let you mutate (well, appear to mutate) the collection? ... I guess that would be if you then assume the changes you make have an "action at a distance" effect on something:
final map = getEmployeeMap();
map['newEmployee'] = new Employee(); // CopyOnWriteMap does a copy here
final moreMap = getEmployeeMap();
// I think moreMap should contain 'newEmployee'
But for me that would be a surprising thing to do. Not sure. What do you think?
from built_collection.dart.
I think copy on write at least gives the API producer safety, but the consumer will need to read the API documentation to know changes they are making effectively do nothing. That being said, I was more concerned about API producer side, so maybe this isn't a real priority for now.
from built_collection.dart.
I think it does make sense to have both, to give people a choice.
This should be trivial to implement, we can use UnmodifiableListView/UnmodifiableSetView/UnmodifiableMapView.
from built_collection.dart.
Related Issues (20)
- Release for inference change HOT 1
- Add const BuiltList<T>.empty() HOT 6
- Add ListBuilder.indexOf HOT 1
- Allow BuiltList<T?> HOT 2
- BuiltList first & elementAt should be nullable HOT 3
- Custom serializer for BuiltMap HOT 1
- Tighten generics HOT 2
- Use `@checkResult` annotation
- Cannot rebuild a BuiltSet entry in BuiltMap HOT 7
- (Question) how to rebuild every value in BuiltList? HOT 1
- Why is the return operator[] on BuiltSetMultimap nullable?
- Docs for `asList` misleading
- Unable to update nested BuiltList inside ListBuilder HOT 5
- [Feature Request/ Question] Native Support for json_serializable / json_annotation HOT 3
- Found 1 file excluded from sound null safety HOT 1
- Shouldn't BuiltMap<String, BuiltSet<int>> work out of the box? HOT 9
- BuiltMap.containsKey should accept `Object?` HOT 1
- Built Collections deep comparison question HOT 3
- asList() always returns a new instance HOT 1
- tighten `MapBuilder` factory parameter HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from built_collection.dart.