-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Never use as
.
#57241
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
First cut at some descriptive text. From the flutter style guide: AVOID using If you know the type is correct, use an assertion or assign to a more narrowly-typed variable (this avoids the type check in release mode; BAD:
GOOD:
|
The "GOOD" for that "BAD" would be more like: Person person = pm;
person.firstName = 'Seth'; The "BAD" for that "GOOD" would be more like: try {
(pm as Person).firstName = 'Seth';
} on CastError { } |
Updated. Thanks! |
This seems to conflict with strong mode options.include = JSON.decode(include) as Map<String, List<String>>; // warning: avoid as to Map<String, List<String>> json = JSON.decode(include); // error: will need runtime check
options.include = json; just to get the warning that it will need a runtime check. |
We filter out that strong mode warning. We don't actually use strong mode itself (i.e. DDC and its dart-to-js conversion). We do turn on the strong mode warnings in the analyzer, but only because they happen to catch some things we want to catch. They also catch things we don't want, and we have a script that filters those out: Ideally we'd have all the warnings we could want with strong mode off. It's not our goal to influence the DDC work, which has a very different purpose than what we're doing. |
@Hixie thanks a lot for explaining your take on strong mode. |
if the advice is "never use as", then maybe "as" should be removed from the language? |
@stt106 that was for Dart 1. This is quite a different story in Dart 2. |
@zoechi Then why in https://raw.githubusercontent.com/flutter/flutter/master/analysis_options.yaml it still says "avoid using as" ? |
We (Flutter) haven't removed that lint yet because we want to remove the lint at the same time as we update all the code to use |
in the example if (pm is Person)
pm.firstName = 'Seth'; BAD: try {
(pm as Person).firstName = 'Seth';
} on CastError { } I would rather prefer if (!(pm is Person)) {
// throw or early return
}
pm.firstName = 'Seth'; Should I create a new issue asking for this enhancement, as currently linter is not able to understand this? |
Try if (pm is! Person)
throw Exception('...');
pm.firstName = 'Seth'; ...but note that that is a different semantic than the GOOD vs BAD case in the documentation. The point of the documentation's example is that a non-matching (and non-null) value will not result in an exception, it'll just be ignored. (In the case of the value being null, it'll throw in the GOOD case and not in the BAD case.) In general, all three of these techniques aren't very good since they introduce knowledge of the class hierarchy outside of the interface defined by the class hierarchy, which breaks polymorphism. |
thanks, I didn't know about
Btw, I think what i'm trying to achieve is similar to https://docs.swift.org/swift-book/ReferenceManual/Statements.html#grammar_guard-statement , which might be out of this issue's scope, or even linter's scope, but more into the language spec itself. |
As per #57238.
The text was updated successfully, but these errors were encountered: