The Swift guard statement allows you to unwrap optionals to a new constant and do an early return if the assignment fails.
var someString:String? = "hello"
...
...
guard let newString = someString
else {
return
}
...
If I want to unwrap an optional and set it to a pre-defined non-optional variable, I've been first unwrapping to the new constant (newString) and then setting the non-optional variable after the guard statement like this:
var someString:String? = "hello"
var nonOptionalString:String = "bonjour"
...
...
guard let newString = someString
else {
return
}
nonOptionalString = newString
...
Is there a way to set a pre-defined, non-optional var within the condition of the guard statement without creating a new constant? Something like the following (which doesn't work)?
var someString:String? = "hello"
var nonOptionalString:String = "bonjour"
...
...
guard nonOptionalString = someString
else {
return
}
...
If something like this isn't possible, is there an underlying philosophy behind the Swift language design or technical reason as to why this doesn't exist?