-
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
refine no-implicit-dynamic to allow inference from explicit dynamic #26781
Comments
CC @leafpetersen @munificent -- let me know if those examples look right (you'll get another chance to review it in CL form :) ) |
I thought we had come down the other way on dynamic d;
var x = d; but I could be misremembering. I'm fine with the categorization above, but I'm not sure whether I like calling this categorization |
Sounds good to me. Moved that one back. How do you feel about the upwards inference examples? |
Status update: need to step back and work out a design for this starting from the use cases. For the moment I think it makes sense to expose the other two flags: no-implicit-casts is very simple and cleans up the type system (assignability relation is just subtype), and no-implicit-dynamic, while a bit blunt, operates in a simple way and doesn't depend on strong mode. It might be too noisy, but we can see what the feedback is like. I view this bug as a request to blend a static typing+inference and dynamic typing+inference in a way that makes both feel natural to use. I think that can be done (e.g. C# dynamic does it pretty well IMO), but it needs a bit more thought. |
@nex3 reported being unhappy with no-implicit-dynamic here: #27624 The gist is that we should be able to infer from an explicit dynamic. However, as illustrated by examples above it's not clear where to draw this line. My personal advice would be to not use dynamic, unless you want dynamic dispatch. Prefer |
I'd consider all the examples listed as "not errors" above not errors. I would also prefer that Leaf's example not be an error.
This seems contrary to the spirit of this style guide entry, which indicates that Another option would be to update the style guide. |
yeah that style entry is pretty old. There's a note somewhere that we need to refresh the style guide in the context of Strong Mode/Dart 2. I think that particular entry made sense in Dart 1 where error checking was almost non-existent anyway. Nowadays |
It looks like converting to Object doesn't really work, though: #27625 |
I do think it's useful for API designers to be able to signal "This accepts any and all objects and will handle them all fine" distinctly from "This accepts certain objects, but I can't express which ones in the type system". But if end up losing that, it's not the end of the world, I suppose.
My assumption is that most APIs that accept dynamic because there isn't a better type to specify (like a union) generally cast that object to one of a few more precise types in their body to get back to a more statically-typed realm when possible. But now that you mention it, my assumption may be wrong. Maybe we should use |
I personally have never wanted, or really even seen a compelling use-case for, runtime dynamism in Dart. Keeping it as the default behavior for cases where a type is unspecified and can't be inferred seems less than optimal. |
There are other bugs now tracking improved strictness flags, we do plan to refactor bits of |
no-implicit-dynamic feels too restrictive. Is there some way we can make dynamic code concise while still preventing it in places where it was not intended?
The text below was the original proposal -- some of this may be obsolete. Needs review.
In any position where a type could be inferred, report if inference fails. Inference here includes downward inference and variable initialization, TBD other cases like
[42 as dynamic]
.Some examples where an error would be reported:
These would not be errors:
There is a bit of a debate around upwards inference cases, in particular the
[d]
,f(d)
, andnew withArg(d)
examples above. My inclination is to accept them, as we already have some stricter flags (no-implicit-dynamic) that would reject.Original context: #25573 (comment)
The text was updated successfully, but these errors were encountered: