-
Notifications
You must be signed in to change notification settings - Fork 336
Allow normal Swift default property initialization syntax #170
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 normal Swift default property initialization syntax #170
Conversation
This change allows the normal `var foo = "blah"` default initialization syntax for `Option`s, as a parallel initialization method as using the `default` parameter.
I've tested this with this simple change, and it seems to work as expected (and all the test cases still pass locally); however, I want to make sure this is something that would be desired before continuing to extend this to the other parameter types. |
A simple implementation to test: import ArgumentParser
struct SomeCli: ParsableCommand {
@Option
var data: String = "test"
func run() throws {
print(data)
}
}
SomeCli.main() $ swift run cli
test
$ swift run cli --data=test2
test2 |
@swift-ci Please test |
Looks like everything passed in CI, so is this something that would be desired for the remaining parameter types? |
Thanks for starting this implementation! It’s been on my to-do list to investigate, but I apparently hadn’t opened an issue. I have a couple in-flight changes that you might want to wait for, most notably removing The biggest downside I see to this change is that it can effectively double the number of inits in some cases, where the |
Given those concerns, would it maybe make sense to drop the current And honestly I’ll probably push ahead with the development on this more just as an exercise for myself, so don’t rush your other changes on my account; once they’re done, I can always integrate them back into this. |
I think this is a great direction |
I definitely agree that we'd just want the one syntax long term. To help existing users transition, we'll want to go from the status quo to three different inits: // current
init(default: Value? = nil, ...)
// new
init(wrappedValue: Value, ...)
init(...)
@available(*, deprecated, message: "Use regular property initialization for default values")
init(default: Value?, ...) There's ample room for code deduplication in the initializers, but that can come after this change.
That change landed in #173, so you've got a clear field for this. Thanks again! |
Preparing for another no-initial value `init` to be added and the existing one with a `default` parameter to be deprecated
It's replaced with an `init` containing no default value parameter, which will be used when the user does not provide any value. Also add a (most likely unnecessary) sanity test to make sure initializations without a default value still work. Also copy out documentation to allow clean removal of the older `init` when the time comes.
…sform` parameters
I'll continue work on this tomorrow/over the next week, but I believe |
I just submitted #186, which adds the I like this direction for initialization over the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking great, @MPLew-is! One note for you below, plus the documentation should read as if using the regular assignment syntax was always the way to do this — we'll handle the notes about old/new and migration in the changelog.
This mirrors the non-transform variants, and should have been included in the previous commits
Private `init` doesn't need defaults, and the deprecated public ones shouldn't have it to avoid confusion with the new methods
Treat new initializers as the originally intended way to allow for clean removal of the deprecated methods Also add some additional documentation to the deprecated methods to help point users in the right direction
Other than the issue I've pointed out in a line comment, this should be functionally complete. I'll work on the high-level documentation next, but please let me know if you see any other changes I should make! |
Prints a warning when that default value is `true` instead of `false`, as the flag value will be pinned regardless of user input
All examples and unit tests have been transitioned to the new syntax, with the exception of `SourceCompatEndToEndTests`, which should not have the old style removed until it is no longer valid source.
This should be ready for review, let me know if you find any issues! |
@swift-ci Please test |
1 similar comment
@swift-ci Please test |
@swift-ci Please test Linux platform |
This change allows the normal
var foo = "blah"
default initialization syntax forOption
s, as a parallel initialization method as using thedefault
parameter.This syntax may feel more natural to some developers (myself included), but does not break any existing code using the original initialization method.
Checklist