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

Allow default nil values for optional properties #480

Merged
merged 4 commits into from Aug 31, 2022

Conversation

natecook1000
Copy link
Member

This adds underscored initializers that let library users add = nil to declarations of optional @Option and @Argument properties. Previously, default values have been available for properties of non-optional types only.

These new initializers use _OptionalNilComparisonType as the wrapped value parameter, so only a nil literal is acceptable in the default value position. This avoids the problem of declaring an optional property with a non-nil default, which ends up negating the purpose of an optional.

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

This adds underscored initializers that let library users add `= nil` to
declarations of optional `@Option` and `@Argument` properties. Previously,
default values have been available for properties of non-optional types
only.

These new initializers use `_OptionalNilComparisonType` as the wrapped
value parameter, so only a `nil` literal is acceptable in the default
value position. This avoids the problem of declaring an optional property
with a non-`nil` default, which ends up negating the purpose of an optional.
These are sprinkled in where they seem natural, to illustrate how
types can be initialized.
@natecook1000
Copy link
Member Author

@swift-ci Please test

@natecook1000 natecook1000 merged commit 0bac2cc into apple:main Aug 31, 2022
@rauhul
Copy link
Contributor

rauhul commented Aug 31, 2022

Just wondering, why is this desired? Are we expecting an upcoming language mode where var x: T? doesn't have a default value of nil? (I would love that change)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants