-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Hint and/or lint for changing List/Set/Map from
to of
#58359
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
Comments
Does the same advice extend to |
Currious : is If you look at https://api.dart.dev/stable/2.8.4/dart-core/Iterable/toList.html you can see that the implementation relies on |
And |
The advice extends to @a14n: Yes, we should change that @a14n: Regarding They are different in that the run time type value Reasons to use
Reasons to use
|
We might also consider folding this advice into Effective Dart? |
Isn't one of the main benefits of using void parse(Map<String, dynamic> json) {
for (final MapEntry<String, dynamic> entry in json.entries) {
print("${entry.key}: ${entry.value}");
}
}
void main() {
final Map json= {"hello": 1, "world": true}; // typed as Map<dynamic, dynamic>
parse(Map<String, dynamic>.of(json)); // Error: Map<dynamic, dynamic> is not Map<String, dynamic>
parse(Map<String, dynamic>.from(json)); // okay
} |
Seems like a duplicate of #58149? |
Well that was created later than this, and the discussion is here, so technically #58149 is a duplicate of this |
Huh? This issue seems to have been created in March 2021 (unless transferring the issue modified the timestamp?). #58149 was filed last year. =/ |
Oh sorry, I didn't see the years, just "March" and "April" |
I'm seeing this issue in Flutter framework code - using from where we reallys hould use of because of extra type checking that is incurred by from. |
And |
@Levi-Lesches to answer your question, which holds with @rakudrama 's initial suggestion, is that we would not report a |
See also #57106 |
List.from
to List.of
from
to of
Many uses of
List.from
can be changed toList.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 efficientList.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):
E
inList<
E
>.from(iterable)
matches the static type of the iterable argumentE
inList<
E
>.from(iterable)
is a supertype of the static type of the iterable argumentMore complicated is where
dynamic
creeps in.I have seen examples like:
If we change
List.from
toList.of
, the inferred type ofkey
moves fromdynamic
toString
. This would be helpful to the developer for completion suggestion, sincekey.isE•
can now only complete tokey.isEmpty
, and notkey.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).
The text was updated successfully, but these errors were encountered: