Skip to content
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

Warn when implicitly using 'dynamic' #24712

Closed
3 tasks
Hixie opened this issue Oct 23, 2015 · 4 comments
Closed
3 tasks

Warn when implicitly using 'dynamic' #24712

Hixie opened this issue Oct 23, 2015 · 4 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@Hixie
Copy link
Contributor

Hixie commented Oct 23, 2015

I'd love to see a way to enable warnings from the analyzer in the following cases:

  • Any time where a 'dynamic' is implied in such a way that it affects the resulting object's runtimeType

For example, [1, 2] has a different runtimeType than <int>[1, 2].
Similarly, Map<int, int> a = new Map() has a different runtimeType than Map<int, int> a = new Map<int, int>()

This is important for anyone who uses is with generics, e.g. if (foo is List<int>) { ... }.

  • Any time an argument, field, local variable, or method return value has no explicit type.
  • Any time the code uses var.

(Explicit use of dynamic is fine, there's times where it's just what you need.)

@bwilkerson bwilkerson added Type-Enhancement area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels Oct 23, 2015
@sethladd
Copy link
Contributor

if (foo is List<int>)

Do you ever do this in Flutter?

cc @iposva-google

@Hixie
Copy link
Contributor Author

Hixie commented Oct 26, 2015

Yes, a lot. Usually in the form:

   class Foo<T> {
      bool bar(dynamic x) => x is T;
   }
   // ... 
   ... new Foo<List<String>>() ...

...or whatever. The easiest-to-explain example of this is the drag-and-drop system, where you say what kind of data type you'll want to receive. It only works if anywhere that you add data to the drag system, you do it in a fully-typed way, otherwise everything will be recognised as a match even if it isn't.

(The other thing we do all the time is compare runtimeTypes of specialised types. That one happens even more. For example, we might compare a ScrollableList<Stock> to a ScrollableList<Company>. If either had been built with an implicit dynamic, it would count as a match and the results are unlikely to be pretty.)

@leafpetersen
Copy link
Member

We've discussed this for strong mode. Related issues here:

dart-archive/dev_compiler#363
dart-archive/dev_compiler#365

For your purposes, I think you'd want us to turn off inference of type arguments (or at least provide you with feedback that we have done such inference) since you are running on the VM and hence the inferred types would not actually be reified as such at runtime.

@jmesserly
Copy link

FYI - I'm addressing these sort of issues now. Splitting this off into a few specific features that we've identified:

#26583 -- flag for disabling implicit casts
#25573 -- flag for disabling implicit dynamic

Let me know if there's more that isn't captured here. We think those flags will go a long way towards "stronger" Dart. Especially no-implicit-dynamic, which catches all kinds of things (dynamic dispatch + dynamic ending up in generic methods/classes).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants