-
Notifications
You must be signed in to change notification settings - Fork 1.7k
consider pointing to required keyword when reporting errors for non-nullable named parameter without default value #44329
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
I understand the @required parameter, but I was talking about optional parameters. I have a lot of classes with the following code
The issue is actually an assumption I had about Dart in general which turns out to be wrong. I just assumed that if Y is not set in the constructor, then Dart would not bind it to the field and the field would keep the initialized value. That's not the case, Dart will bind null to the value and the non-nullable Dart just surfaced the issue with the error message you posted. With nnbd I would have to do as below, but that's often not possible because Y does not have a const constructor, and often it can't have one.
Which means that with NNBD my classes can't have optional constructor parameters, or most of my fields (everything that is not a basic type) have to be nullable. Or I have to rely on hacks like below, to get something that resembles non-nullable fields:
Which is a big step backwards, and it makes migrating to nnbd quite difficult. |
@Rudiksz, your topic may be a separate issue, but I'll comment here because this comment might suffice. Consider the following approach: class B ...
class A {
final B b;
X({B? b}): this.b = b ?? B();
} This will allow |
Thanks for the tip.
This is pretty much what I assumed Dart does under the hood, but apparently I have to do it manually, null-safety or not. |
A request for more flexible default values has been made earlier, for instance, dart-lang/language#140. PS: The topic of this issue is still the error message, as described by @mraleph here. ;-) |
On the topic of error messages: I agree that the message could be improved, and I'll look into doing so. I'd also welcome input from the language team, and anyone else that's interested, to help identify other messages that could be improved, either with or without suggestions for how to improve them. But I do want it to be clear that the analyzer's messages have intentionally been split into two pieces in order to allow IDEs more control over the presentation of those messages. The first is the problem message--a description of the problem that was discovered--and the second is the correction message--a description of possible ways to resolve the problem. In this particular case, the correction message is
which I think addresses at least part of your concern. At the moment the command-line analyzer only displays the correction message if the |
Proposed change in https://dart-review.googlesource.com/c/sdk/+/174300. |
@bwilkerson I agree with that. Do we have an issue tracking that change? |
Not that I'm aware of, no. |
Catching up on this thread; I think this does make sense - always showing the correction message. Separately, I think it would be worth it to do a brief audit of the exiting correction messages to ensure that they do give the user non-trivial advice (isn't something the user could know just by reading the error message or the code). |
Adjusting priority: this is going to be very, very common for null safety, so I think this is a P1, not a P3! |
Note that the message for the diagnostic called out in this issue has been updated by the aforementioned CL. |
Given the work that Brian's already landed for this, and the tweak to the dart analyze output that's in progress (https://dart-review.googlesource.com/c/sdk/+/174564), I think we can close this issue. |
I've created https://dart-review.googlesource.com/c/sdk/+/174800 for the CFE. |
Update in response to issue #44329 and to match the change in https://dart-review.googlesource.com/c/sdk/+/174300 Change-Id: Ia28887f0fad54927b82b7aa29acb88231053cd6a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/174800 Reviewed-by: Vyacheslav Egorov <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
produces in analyzer
This error message seems to indicate that the only way to address this error is to provide non-null default value. However this is not true - you can either provide a default value or use
required
keyword. I think this error message in both CFE and analyzer should be ammended to list both alternatives to be more user friendly.This report is based on user report from Reddit
I think this also indicates that we might want to go through all error messages added in NNBD release and look at them from the perspective of UX and whether or not they are immediately actionable and clear even for the users who are encountering them for the first time. /cc @leafpetersen
/cc @stereotype441 @johnniwinther
The text was updated successfully, but these errors were encountered: