-
Notifications
You must be signed in to change notification settings - Fork 214
Implicitly required formal named parameters #4349
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
That would create a difference between function declarations and function types. typedef IntEater = void Function({int value}); // Function type, parameter is *not* required.
void intEater({int value}) {} // Parameter is (with this proposal) required.
IntEater eater = intEater; // Compile-time error.
void maybeIntEater({int value = 0}) {} // Parameter is optional
IntEater eatOrZero = maybeIntEater; // OK. It would also affect forwarding constructors. class C {
C.req({int a}); // required
C.opt({int a = 42}); // optional
factory C.fwdReq({int a}) = C.req;
factory C.fwdOpt({int a}) = C.opt; // Forwarding constructor must not write default value.
} It would require a way to specify that one of When you say "non-nullable type", that means that generic functions can have parameters that are optional depending on the type argument. void consume<T>({T value}) {}
consume<int>(); // Error
consume<int?>(); // OK? Or does it use the declared type, not the actual parameter type, which in this case is the type parameter I'm not unsympathetic to doing something here, but it's not as easy as this. |
I'm missing something here, why doesn't the
As they are written, both forwarding constructors have
Yes, precisely. |
Read a bit some messages on those threads. You're an advocate of the equivalency between "null actual value" and "no actual value" getting passed to a function. But, since in Dart there's the notion of nullable type, I strongly believe "null actual value" and "no actual value" are to be treated alike only when the declared parameter type is non-nullable. That's just to allow the programmers do their own design, whether you consider it poor or not. If the type is nullable, then the function body should expect to receive a |
There are at least two issues similar to this one. One of them was opened during the null-safety development, and was closed. The other is still open and IIRC is slightly different but on the same line. GitHub's search is just terrible. Edit: Found one of them, #878. |
If I compile something like
the compiler complains that
The parameter 'a' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
, while it should simply understand that the named parametera
is required.Proposal
A formal named parameter that has a non-nullable type and no default value, should be understood as being mandatory. I am surprised that no one has already asked for that (at least I did not find a relevant issue).
The
required
keyword can make a difference only in the case of a formal named parameter without a default value, but which has a nullable type. Specifically, it would force the caller to actually sendnull
as a value instead of not specifying a value at all for the parameter. But that's hardly programmer-friendly so I deem therequired
keyword should be removed from the language.The text was updated successfully, but these errors were encountered: