Skip to content

Allow default nil values for optional properties#480

Merged
natecook1000 merged 4 commits into
apple:mainfrom
natecook1000:default-nil-values
Aug 31, 2022
Merged

Allow default nil values for optional properties#480
natecook1000 merged 4 commits into
apple:mainfrom
natecook1000:default-nil-values

Conversation

@natecook1000
Copy link
Copy Markdown
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 natecook1000 requested a review from rauhul August 30, 2022 17:03
@natecook1000
Copy link
Copy Markdown
Member Author

@swift-ci Please test

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

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.

2 participants