-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 core collections stronger & consistent #30405
Comments
All of these are there for one reason or another. Set<Object> x = new Set<int>()..addAll([1, 2, 3]);
...
if (x.contains("string")) ...;`. The assignment is valid due to covariant generics. The lookup is statically safe because of the static type of Most other languages, including Java and C#, do not have covariant generics. For a constructor like So, there are reasons for these choices. If we change them back, we'll need to add some other feature to alleviate the pain. |
We stepped on all of this in Vipunen too, when trying to type-check core libraries under the assumption that |
Assuming that |
Sure, but it doesn't mean they should never change.
It didn't make it more useful for me when I almost submitted a production bug. I imagine if you poll your users, almost nobody finds this a useful feature of Dart. In fact, it reminds me more of JavaScript or Python than C# (
Just because Java's aging standard library doesn't isn't a good enough reason IMO.
+1. Opt-in, almost nobody will actually need this, but +1 for allowing opt-in with a cast method.
Fair, but I'd like to see a specific set of methods/constructors specifically dealing with casting instead of it being the default. It's really confusing to our users that
I like this option. |
I'm guessing these are unlikely to occur or should be broken up into individual requests. |
The one that always annoys me is Map.fromIterable. Couldn't it use a generic at least ? Map<String, Booking>.fromIterable(
bookingList,
key: (booking) => (booking as Booking).id,
), |
@cedvdb It can't, actually. Constructors cannot be generic. (I wish they could, dart-lang/language#647). We could make static Map<K, V> fromIterable<S, K, V>(Iterable<S> source, {K Function(S) key, V Function(S) value}) ... It does mean that {for (var value in source) key(source): value(source)} |
Suggestions:
Iterable
bool contains(Object element)
➡️bool contains(T element)
You never want to write this:
I ran into this today when I had a
Set
that used to beSet<A>
, got changed toSet<B>
, but code was never updated the check it for aB
instead of anA
, yet static analysis reported everything was fine.List
List.from(Iterable elements)
➡️List.from(Iterable<E> elements)
I'm not sure if this is blocked on #30355 or not.
List.unmodifiable(Iterable elements)
➡️List.unmodifiable(Iterable<E> elements)
This is a common way to lose reified type information 😢
bool remove(Object element)
➡️bool remove(T element)
Same issue as described above for
Iterable#contains
.Map
Map.from(Map other)
➡️Map.from(Map<K, V> other)
Map.fromIterable(Iterable iterable, { K key(element), V value(element) })
➡️Map.fromIterable(Iterable<E> iterable, { K key(E element), V value(E element) })
Map.unmodifiable(Map other)
➡️Map.unmodifiable(Map<K, V> other)
bool containsKey(Object key)
➡️bool containsKey(K key)
bool containsKey(Object value)
➡️bool containsValue(V value)
The text was updated successfully, but these errors were encountered: