-
Notifications
You must be signed in to change notification settings - Fork 209
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
Null safety: null-aware operator for constructor or function call arguments #1039
Comments
cc @lrhn @munificent @eernstg I know we discussed this, do we have an issue for it anywhere? I have also seen this pattern come up a fair bit in migrated code. |
It would be nice if we could use a wildcard-like parameter, like: SomeWidget(
optionalParameter1: foo ??. OtherWidget(requiredParameter: _, otherParameter: 42),
optionalParameter2: bar ??. someFunction(_, otherData),
) |
There are multiple issues requesting "if expressions" (like #820), which relates to this problem among other things: SomeWidget(
optionalParameter1: if (foo! = null) OtherWidget(requiredParameter: foo, otherParameter: 42),
optionalParameter2: if (bar ! = null) someFunction(/* requiredParameter */bar, otherData),
) |
@spkersten as somebody whose busy working in a codebase where we had to do that a lot...I can tell you, the better solution is to clean your data up, I've spent the last four months typing up our Node API to get around this issue...and I don't regret it one bit :) |
@Ferdzzzzzzzz What do you mean by "clean up"? Some data is optional. |
@spkersten optional data often means that the data isn't very well thought out, the goal is usually to make illegal states unrepresentable, it sounds strange but when you really dive deep into the data you can usually find a way to remove nulls from the equation :) |
@Ferdzzzzzzzz The example in that article is about several optional fields in a data type which have some implicit conditions about which might be none. I agree that using sum types is better in that case (if Dart would support it in a convenient way). However, there are valid cases where a field is optional. |
There are several different issues requesting a solution to different aspects of the same problem:
It all boils down to wanting to only evaluate some expression if a particular part of it is non-null. The simplest approach is the one proposed here, where all you get is a guarded promoting short-circuit check so you can gate on the nullability of a single variable: int? foo = ...;
var z = foo ?! (foo + 2); // equivalent to `foo == null ? null : (foo + 2)`. This would promote It won't help you as much with non-promotable variables or non-variable expressions. |
GCC has the conditional expression. in GCC
If I could go back before that Dart get
because I came from mainly C background. |
Note that there's some overlap between these "non-null aware" expressions and some existing null-aware operations: extension on int {
int plus(int other) => this + other;
}
void main() {
int? foo = ...;
var x = foo?.plus(2); // Same as `foo ?! (foo + 2)`.
} For syntactic reasons We could use anonymous methods to express the same thing (still avoiding evaluation of the receiver expression void main() {
int? foo = ...;
var x = foo?.=> this + 2;
foo?.{ print("Result: $this plus 2 is ${this + 2}"); };
} |
We have this pattern a lot in our code:
It would help readability if there were a similar null-aware operator like for
a?[1]
andfoo?.bar
. Or another way to avoidfoo == null ? null : ...
.The text was updated successfully, but these errors were encountered: