Skip to content
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

Try to clarify optional parameter restrictions #2621

Merged
merged 3 commits into from
Sep 14, 2020
Merged

Try to clarify optional parameter restrictions #2621

merged 3 commits into from
Sep 14, 2020

Conversation

jamesderlin
Copy link
Contributor

The Language Tour currently states:

Optional parameters can be either named or positional, but not
both.

That statement can be interpreted to mean that any individual
parameter cannot be both named and positional. However, the actual
restriction is stronger: a function cannot have both optional named
and optional positional parameters (see
dart-lang/language#1076).

The Language Tour currently states:

> Optional parameters can be either named or positional, but not
> both.

That statement can be interpreted to mean that any individual
parameter cannot be both named and positional.  However, the actual
restriction is stronger: a function cannot have both optional named
and optional positional parameters (see
dart-lang/language#1076).
@googlebot googlebot added the cla: yes Contributor has signed the Contributor License Agreement label Sep 3, 2020
@kwalrath kwalrath requested a review from lrhn September 3, 2020 22:24
Copy link
Contributor

@kwalrath kwalrath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The writing LGTM, but I'll defer to Lasse as to correctness.

@@ -1129,7 +1129,7 @@ is sometimes referred to as _arrow_ syntax.

A function can have two types of parameters: _required_ and _optional_.
The required parameters are listed first, followed by any optional parameters.
Optional parameters can be _named_ or _positional_.
Optional parameters can be either _named_ or _positional_.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically all wrong for Null Safety. We can have required named parameters placed after optional named parameters.

How about:

A function can have any number of required positional parameters.
These may be followed either by optional positional parameters,
or by named parameters.
A named parameter can be either optional or required.

If this is describing pre-Null Safety behavior, then I'd say:

A function can have any number of required parameters.
These may be followed either by optional positional parameters,
or by optional named parameters.

@@ -1139,7 +1139,8 @@ Optional parameters can be _named_ or _positional_.

### Optional parameters

Optional parameters can be either named or positional, but not both.
A function can have optional positional parameters or optional named parameters,
but not both.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For null safety, we'll need to remove the "optional" before "named".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Lasse.

@kwalrath
Copy link
Contributor

kwalrath commented Sep 8, 2020

We should have a standard string to mark where we know things have to change for null safety. Maybe just NULLSAFE. Something like this:

{% comment %}
NULLSAFE: Remove the "optional" before "named".
{% endcomment %}

Adjust wording to require fewer changes when Null Safety becomes
enabled by default.
@jamesderlin
Copy link
Contributor Author

I've made adjustments to try to be friendlier for Null Safety when it becomes enabled by default. Notably I've removed the "Optional parameters" subheading since optional/required and positional/named will be independent axes. I've instead added a more general "Parameters" heading and merged part of the "Functions" section into it.

Copy link
Contributor

@kwalrath kwalrath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we can simplify this a bit and also make some of the future changes now.

A function can have two types of parameters: _required_ and _optional_.
The required parameters are listed first, followed by any optional parameters.
Optional parameters can be _named_ or _positional_.
### Parameters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this section head! I don't know why we didn't have it before.

Comment on lines 1132 to 1135
A function can have any number of *required positional* parameters. These may be
followed either by *optional positional* parameters or by *optional named*
parameters. A function can have optional positional parameters or optional
named parameters, but not both.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be a little shorter (and also we avoid may). Also, we should switch the order to match the section names... and maybe it isn't necessary to say "optional" before named — especially since we don't use "Optional" in the section name?

Suggested change
A function can have any number of *required positional* parameters. These may be
followed either by *optional positional* parameters or by *optional named*
parameters. A function can have optional positional parameters or optional
named parameters, but not both.
A function can have any number of *required positional* parameters. These can be
followed either by *named* parameters or by *optional positional* parameters
(but not both).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

{% comment %}
NULLSAFE: Replace "optional named" above with just "named" and add:

A named parameter can be either optional or `required`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we add this now? Something like this.

Named parameters are optional unless they're specifically marked as required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I added that to the "Named parameters" section.

@kwalrath kwalrath merged commit 3846432 into dart-lang:master Sep 14, 2020
@jamesderlin jamesderlin deleted the jamesderlin/optional-parameters branch September 14, 2020 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Contributor has signed the Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants