Skip to content

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

2 participants