-
Notifications
You must be signed in to change notification settings - Fork 30
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
Consider adding always_declare_return_types
#202
Comments
always_declare_return_types
I don't think it's something we should add. We do have the "type public API" lint in the recommended set (I think, or is it just the flutter lints?), so this is only really about local and private functions. At that point it's no longer a recommendation about public API, so it would be because omitting types of particularly error prone. I don't think it is. Local inference is more efficient than top -level inference, and if getting dynamic is a problem, that's what "strict inference" is for. It's a perfectly good lint for enforcing a particular style, which some people may prefer, but it's not the official Dart style, so we shouldn't be enforcing it in the official lints. |
cc'ing in @natebosch and @goderbauer for thoughts as well |
I think I'm in favor to help avoid the implicit Edit: There is one place where this doesn't involve an implicit dynamic which is then even more of a style question - overrides can safely omit return types. I'd still be OK with adding this to recommended. |
It looks we do recommend this in effective dart:
(in addition to the recommendation, "DO annotate parameter types on function declarations") For some other data points, this is enabled in package:very_good_analysis (a popular lint set), has been enabled in the dart_flutter team set for a while, is in the flame lint set, and as the original poster mentions, was in the pedantic set. |
From the Effective Dart entry:
That's not true for local functions (any longer, and for quite some time), the language actually does infer return types. There is a difference between local and top-level inference (mainly because top-level declarations can be mutually dependent and can introduce types). Local inference is more powerful than top-level inference, and this is one of the places where it's visible. topfoo() => 42;
topbar(bool x) {
if (x) return 3.14;
return 42;
}
void main() {
foo() => 42;
bar(bool x) {
if (x) return 3.14;
return 42;
}
print(foo.staticType); // () => int
print(foo.runtimeType); // () => int
print(bar.staticType); // () => num
print(bar.runtimeType); // () => num
print(topfoo.staticType); // () => dynamic
print(topfoo.runtimeType); // () => dynamic
print(topbar.staticType); // () => dynamic
print(topbar.runtimeType); // () => dynamic
}
extension <T> on T {
Type get staticType => T;
} That's why I think it's an unnecessary lint for local functions, and therefore it's too strict to be included. The You can also declare instance methods without types, which are then inherited from the superinterface. abstract class I {
int _foo(int x);
}
class C implements I {
_foo(x) => x + 1;
}
void main() [
print(C().foo.runtimeType); // (int) => int
} Again the lint is asking you to write something that the language doesn't require, only because the style guide says so, and the style guide's argument doesn't agree with reality. And again, The I don't think we should enable this lint. The (aka. my) reasons for including a lint is help authors:
If you follow that, we should be happy. What you do inside the bodies of your functions is not our business, as long as it does what it looks like it does. And TL;DR: Not this lint. Maybe |
I didn't know that local functions get an inferred return type. That does change my opinion significantly. I agree with Lasse - it would serve our users better to steer them away from implicit dynamic, and allow implicit not-dynamic if the author prefers. |
For clarity, when you refer to the analyzer:
language:
strict-inference: true |
I probably mean |
@vincevargadev, thanks for the proposal. Some notes from a discussion:
Briefly, we think we want a new lint, which warns for omitted return types or parameter types, where inference would default those omitted types to |
Opened here: dart-lang/linter#5115. |
Describe the rule you'd like to see added and to what rule set
I'd like to see
always_declare_return_types
included in the rules.To what rule set? Ideally core (but I'll take whatever I can get 😸).
If there is a good reason not to include this rule, it would be great to document that, I couldn't find why it isn't included.
Additional context
passsy/dart-lint
(lint)google/pedantic
I've seen this rule mentioned in the following issues, but I didn't see any of these issues actually addressing why this rule is no longer included, it looks to me that it was simply dropped without any explanation
The text was updated successfully, but these errors were encountered: