-
Notifications
You must be signed in to change notification settings - Fork 1.7k
double.clamp should return a double #39652
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
Related to #30403 where a comment from @floitschG is:
@lrhn will there be changes happening with NNBD? |
Those changes sadly did not make it into Dart 2.0. We are aware that some number operations have become very cumbersome ( The current |
You could solve issue 2 above by implying Issue 1 is going to become seriously problematic with NNBD. We're already experiencing this in Flutter to the point that we're discussing creating an extension method on |
Making In practice, I wouldn't bet on any code actually breaking. The Even with special typing rule exceptions, we also need the type inference to understand those exceptions. Otherwise we'll still infer Adding specialized methods to |
I'm falling into toDouble() hell when it comes to clamping doubles :-( |
The null safety rules now say that the static type of clamp is double if any argument is Edit: Actually, the final design has the return type be |
I ran into two issues independently recently that both led me to the same conclusion:
double.clamp
should just always return adouble
.Issue 1: removal of implicit casts (cc @a14n @goderbauer)
Without implicit casts,
double.clamp
needs continuous babysitting withas
to make it work:It's absurd that you keep having to cast things to double when you are only dealing with doubles.
This is newly important since implicit casts are going away with NNBD.
Issue 2: Accidental ints
Because int literals can masquerade as doubles, it's common to use them. Because
clamp
takes nums, they don't get considered as doubles there. Becauseclamp
returns its value directly, what appears to just be code dealing in doubles is actually suddenly dealing with ints, which leads to runtime errors:The text was updated successfully, but these errors were encountered: