-
Notifications
You must be signed in to change notification settings - Fork 213
Provide better support for default function parameters #2899
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
IMHO, #2232 is better. |
@Cat-sushi I may have easily missed something in that issue while skimming over it (the issue is related, thanks for linking it), but there seems to be no way to pass |
#2232 is not a alternative, but a counter-proposal of this issue.
No, we can't , and I think we shouldn't. |
Here's an example where #2232 breaks (as far as I can tell) and you're stuck with both of the above workarounds I mentioned. I am encountering this issue right now, and thus why I opened the issue. Say you are designing a package with an API represented as an object that is given to package consumers: final class Api {
// ...
} And that you can compose extensions based on the API that give it some new concrete functionality (based on everything given in the API): typedef ApiExtension<R> = R Function(Api); Now say that someone (maybe in an entirely different package) wants to extend this API with an extension, like allowing the API to react to typedef FutureWatcher = SomeValueWrapper<T> FunctIon<T>(Future<T>, {T initialData});
FutureWatcher watchFutureExtension(Api api) { ... } Now, take a peek at the But now, say it is custom in the package to provide extension methods on something like extension FutureWatcher on Api {
SomeValueWrapper<T> watchFuture(Future<T> future, {Option<T> initialData}) { ... }
} Here, it is an extension method so we can't even do the default object approach! Instead, we force users to pass in an explicit value for Option whenever they want to use the extension method. With the current state of default parameters, you have issues like this where resulting APIs are going to be broken, because null can semantically mean something completely different than a default. |
If we had union typing, as per #2232 (comment), then this would be a non-issue. However, we don't have union typing. |
I think the issue with #2232 really just boils down to the fact that From #2232:
Those are inherently different scenarios, at least when thinking about generics/user-specified types that as a package author have no control over. I do, however, like the idea behind #2232 with the better function parameter grammar, but the way to do so should not be through null in my eyes; it should be through a different special type. |
I looked through some other issues, but (to my surprise) did not find what I am looking for.
TL;DR: I am looking for syntax like the following:
Nullable default parameters work great until you have a type
T
that is nullable that you need to pass into a function.Dart has no support for such a situation.
There are two workarounds (at least that I am aware of), both of which have some problems.
But this forces you into inheritance and some really ugly generic work. What happens if you just need a function? Perhaps you can use a callable class, or a function typedef, but both of those require another workaround too.
Option
or similar class from functional programming.However, this requires users to explicitly wrap the parameter in
Some(myValue)
, and requires a dependency on someOption
implementation.Each of these workarounds has drawbacks.
Any sort of
copyWith
method (amongst many others) run into this issue when handling nullable members, and is forced to implement one of the above.Perhaps this proposed default super type would act similar to null, with a builtin type union?
The text was updated successfully, but these errors were encountered: