Open mateusfccp opened 1 year ago
If I open a PR for this, will it get accepted?
Sorry for the delayed response. I don't think a PR for this would be accepted yet. We must have a consensus that it would be a high value lint.
Just as a comment, I think this lint still be at least as valuable as prefer_int_literals
.
Both are entirely style based, there is absolutely no semantic difference between using a decimal integer literal and a double literal with the same value.
That might not be "high value" on an absolute scale, but if the two lints can share any code, the effort to add the second lint should be lower than the first.
So potentially bigger value and lower cost should make this a slam-dunk if evaluated with the same measures as prefer_int_literals
.
I personally find integer literals less readable, because they don't tell mere that the value is actually a double
, where other people likely find the .0
to be unnecessary noise.
It's mainly subjective taste, and a choice of style for consistency.
I'd personally use this lint, and wouldn't use prefer_int_literals
.
In our team we use Flutter style guide as our style guide, and I was surprised that there was no lint to support it, just the opposite.
That might not be "high value" on an absolute scale, but if the two minutes can share any code, the effort to add the second lint should be lower than the first.
This is exactly what I was thinking. The code will probably be mostly the same, so it does not seems costful to implement.
Well, it'd probably be accepted then. We don't meet as a team to accept or reject proposals. The number of people you need to convince is on the order of 1 😛 . I won't guarantee it'd be accepted.
To be transparent: I really don't like our lack of process here either. We've responded to PRs before with "whoa, let's get consensus on a proposal first." But if someone proposes a lint rule, we also have no process for reviewing it; the vast majority languish.
@lrhn @srawlins
I just opened a PR in https://dart-review.googlesource.com/c/sdk/+/337842
Any update on this?
None since the last comments on the PR.
The way I'd scope this feature would be to make it precisely the opposite of prefer_int_literals
, in that it triggers for precisely the same expressions, numerals with integer values in a double
-expecting context, but instead of requiring an integer numeral, with no fraction or exponent, it prohibits it.
prefer_double_literals
Disallows using a decimal integer numeral where it will be interpreted as a double literal.
Instead use a floating point numeral with a fraction and/or exponent part, even if it is only a trailing .0
.
Since Dart 2.1, an integer numeral (decimal digits with no fraction or exponent part) in a context where a double
value is required, is interpreted as a double literal.
For instance, one can do final double a = 1;
instead of final double a = 1.0;
, and it means precisely the same thing.
This lint insists on having non-integer numeral in those positions, to make it explicitly visible that the value is
a double
value, not an int
. That means a floating point numeral with at a fraction part and/or an exponent part. Typically this is ensured by appending .0
to the integer numeral.
The lint prefer_int_literals
requires using an integer numeral where a number literal has an integral value and
will be interpreted as a double value.
This lints requires using a floating point literal in the same places.
An example of this is in the Flutter repository style guide itself: Use double literals for double constants
Enforce style advise
final double a = 1;
printDouble(1);
void printDouble(double a) => print(a);
final double a = 1.0;
printDouble(1.0);
void printDouble(double a) => print(a);
prefer_int_literals
;
prefer_double_literals
Description
A lint that warns if someone uses an
int
literal where the target should receive adouble
. Basically, the inverse ofprefer_int_literals
.Details
Since Dart 2.1, we are allowed to pass an
int
where adouble
is expected, and it will be converted to itsdouble
equivalent. For instance, we can dofinal double a = 1;
instead offinal double a = 1.0;
.https://github.com/dart-lang/sdk/issues/34824 introduced
prefer_int_literals
, where the linter suggests doing it whenever possible. However, sometimes the user wants the contrary, because usingdouble
literals makes it clearer that we are dealing withdouble
s.An example of this is in the Flutter repository style guide itself: Use double literals for double constants
Kind
Enforce style advise
Bad Examples
Good Examples
Discussion
prefer_int_literals
;