-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Adding a lint: Extension methods crashed when called on a dynamic object #44270
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
This is by design: https://dart.dev/guides/language/extension-methods#static-types-and-dynamic I have no idea if or how this could be turned into a compile time error - someone from the language team could better answer that. |
It could probably at least be a lint. |
@leafpetersen would know the langauge question, @pq the lint |
For better or worse the contract with dynamic is that the compiler just believes that you know what you're doing. I think this would be a fine candidate for an opt in lint though. |
Yeah. This seems to fit in a general category of advice that recommends avoiding @chickenblood: can you provide some context? Why would you type |
I try to avoid dynamic altogether. In this case I was consuming a Flutter API that returns a dynamic type. https://api.flutter.dev/flutter/widgets/DragTargetMove.html I did not realize this since most of the callbacks for this class are typed I plan to file a bug on that API. However, I would still hope that the linter could help us in cases like this. It is often not apparent that an implicit conversion is taking place. |
There are a couple lints around implicit dynamics. I'm not sure they capture this case. @chickenblood - do you have an example of where the dynamic call is coming from? The |
Sure. I provided a callback for DragTarget.onMove and in there called details.data.doThing() (doThing() being the extension method).
Because details.data is dynamic, the failure occurs. DragTarget.onAccept and DragTarget.onAcceptWithDetails do not have this problem because the argument is typed. The lint sounds like a very good idea here (as well as fixing the DragTarget API). |
@franklinyow - it sounds like the resolution for this issue is for a lint request? If that's the case, I'd update the title to reflect that, and move the issue to the dart-lang/linter repo. |
Issue moved to dart-lang/linter dart-lang/sdk#58289 via ZenHub |
Happens on MacOSX / Linux and reproducible in DartPad.
Dart version: Based on Flutter 1.23.0-18.1.pre Dart SDK 2.10.4
Consider the following code:
This compiles fine, but crashes at runtime because the extension method bar() cannot be found. The compiler should either fail with an error, or better still valid code should be generated that invokes the method at runtime.
The text was updated successfully, but these errors were encountered: