Open
Description
Many uses of List.from
can be changed to List.of
.
The reasons to do so:
List.of
is nicer to use because the extra constraint helps type inference and editor suggestionsList.of
is more efficient- There is an education problem that since
List.of
is newer, there are lots of poor examples ofList.from
that could have been migrated toList.of
.
Examples that could be hints (i.e. no false positives):
- The explicit or inferred type argument
E
inList<
E
>.from(iterable)
matches the static type of the iterable argument - The explicit type argument
E
inList<
E
>.from(iterable)
is a supertype of the static type of the iterable argument
More complicated is where dynamic
creeps in.
I have seen examples like:
void foo(Map<String, String> map) {
for (final key in List.from(map.keys)) {
print(key.isEmpty);
...
If we change List.from
to List.of
, the inferred type of key
moves from dynamic
to String
. This would be helpful to the developer for completion suggestion, since key.isE•
can now only complete to key.isEmpty
, and not key.isEven
.
We would want to suggest the replacement if the new inferred types do not lead to new or different errors or warnings or a different resolution of elements (e.g. a dynamic instance method call becoming a static extension method call).