-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Problem
It is not obvious from a call site whether Foo.bar(...) is a static method or a named constructor. As a result, the return type of Foo.bar(...) is non-obvious.
Proposal
Update the following lints to consider named constructors to have non-obvious types:
omit_obvious_local_variable_typesomit_obvious_property_typesspecify_nonobvious_local_variable_typesspecify_nonobvious_property_types
Example
class MediaQuery {
MediaQuery.removeViewPadding(...) {}
static Widget fromView(...) => ...;
}
// Explicit local variables - types are obvious
final Widget one = MediaQuery.fromView(...);
final MediaQuery two = MediaQuery.removeViewPadding(...);
// After - what's the type of `one`? It's not obvious
final one = MediaQuery.fromView(...);
final MediaQuery two = MediaQuery.removeViewPadding(...);See also: MediaQuery.fromView, MediaQuery.removeViewPadding.
Counterarguments
Changing the behavior of an existing lint
Changing existing lints can be painful. For example, this can cause your project to fail to build when you update your Dart SDK.
However, I suspect that changing this behavior is acceptable as:
- All affected lints are marked experimental.
- All affected lints are used by less than 1% of all users. Google internal dashboard.
Current behavior is beneficial for more experienced developer
For someone aware that the lint is active, the very absence of a type declaration signals that the right-hand side must be a named constructor call whose type is considered unambiguous by the tool. This provides a subtle informational cue, but it was acknowledged that this is an "advanced" reading of the code that relies on prior knowledge of the linting rules.
While this might be true, I'd argue this is orthogonal to the lints' goal of specifying non-obvious types. This named constructor behavior is non-obvious! :)
cc @eernstg