-
Notifications
You must be signed in to change notification settings - Fork 214
add hasValue for null parameters #1064
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 have been fix this by use optional parameters |
Checking for whether an optional parameter was passed or not is actually a feature Dart has already had. Currently, to forward a call to another function with the same signature, you can simply pass the parameter values. The second function will not be able to distinguish whether it got the default value because it was passed deliberately or because it wasn't passed at all. int forward({int a, int b, int c}) => forwardee(a: a, b: b, c: c); If functions has that ability, then forwarding will require passing the exact same parameters, not just the same values. Then forwarding three optional named parameters would require eight different calls: int forward({int a, int b, int c}) =>
if (?a) {
if (?b) {
if (?c) return forwardee(a: a, b: b, c: c);
return forwardee(a: a, b: b);
}
if (?c) return forwardee(a: a, c: c);
return forwardee(a: a);
}
if (?b) {
if (?c) return forwardee(b: b, c: c);
return forwardee(b: b);
}
if (?c) return forwardee(c: c);
return forwardee();
} The solution to that would be an ability to conditionally pass an argument: int forward({int a, int b, int c}) =>
forwardee(if (?a) a: a, if (?b) b: b, if (?c) c: c); That increases the complexity. Even with that, users are likely to forget and make mistakes. It's not impossible, but it's probably not worth it. (At least, I'd make some more changes to parameters in the same change). |
@lrhn So how can I assign a field to null inside the copyWith function? |
You can't. That is annoying, but not something we have considered a big enough pain to make it worth a language change. What you can do is to have two parameters: Foo copyWith({Bar? bar, bool clearBar = false}) => Foo(bar: clearBar ? null : (bar ?? this.bar)); The two parameters are really mutually exclusive, but there is no good way to ensure that you pass only one of them. Another option (pun intended) is to use Foo copyWith({Optional<Bar>? bar}) => Foo(bar: bar == null ? this.bar : bar.isPresent ? bar.value : null);` If you are already using The real problem here is that Dart's nullability is not nested. You can't have Another option would be to twiddle with default values, and allow a parameter to have a different type inside the function than it has in the parameter list. Say: // Signature: Foo Function({Bar? bar}), internal type of `bar` is `Object?` and default value is not `Bar?`
Foo copyWith({Object? bar as Bar? = const _Marker()}) => Foo(bar: identical(const _Marker(), bar) ? this.bar : bar as Bar?); But, to be honest, I'd personally prefer to make the distinction between passing |
Yes. Thank you! |
Common Lisp does something like this and it's pretty nice: (defun foo (&optional (bar 0 is-bar-supplied))
(if is-bar-supplied
(format t "Bar supplied with value ~A" bar)
(format t "Bar defaulted to ~A" bar)) This gives results like this:
|
currently in the copyWith function, if I intentionally want to copy a content with null it will retrieve the old data instead. For example:
I would like to have an additional method to check if that parameter is passed or not. For example:
The text was updated successfully, but these errors were encountered: