-
Notifications
You must be signed in to change notification settings - Fork 1.7k
NNBD: passing optional non-nullable parameter #39897
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 think this is working as intended - if the function wants to accept null as a potential value, then the type of cc @leafpetersen to confirm |
In NNBD, all optional parameters must either have a default value (which is assignable to the parameter's type) or be nullable (which means that it has a default default-value of You can only pass arguments which match the type of the parameter. That means that you cannot pass Passing It does mean that you cannot easily call a function with computed arguments when some of them should be omitted. Not without knowing writing the default value at the call point: Foo(foo: map["foo"] ?? 1, ... qux: map["qux"] ?? "defaultString"); That is not new though. If the function uses non- So, there really isn't any change here except that you have to write a |
@tatumizer It's a cute idea, but it doesn't really work the way you write it. First of all, a value cannot have a static type. Values have run-time types, expressions and variables have static types. |
Taking it further! We could allow An expression e is in a tail position of an expression b if
That means that if It's grammatically specifiable, we just have to have dual production hierarchies: <expression> and <tail-expression> where only the latter can produce |
Maybe we should allow |
You could use maps
|
Not sure where to post it, maybe it's the intended behavior or a bug or an open issue, but ... this doesn't work:
If this is by design, it's very problematic. E.g. we may have a map of 'key:value' pairs and want to create an instance of class Foo by calling the constructor
Foo(foo: map['foo'], ...20 parameters)
, but as soon as there's a single non-nullable parameter, we won't be able to do that - at least, it's not obvious how.This would be equivalent to the invitation to declare all optional parameters nullable.
Pretty sure this problem was discussed somewhere, but I couldn't find any mention of it in the NNBD spec.
(Maybe it's just a bug, and passing
null
is allowed as a token for default value?).EDIT: the proposal of null-elimination operator might help address the issue, but the status of the idea is unclear at the moment.
The text was updated successfully, but these errors were encountered: