[swift-evolution] If let syntactic sugar
Massimo Frasson
maxfrax at gmail.com
Thu Feb 4 09:20:28 CST 2016
Hi everyone,
I find myself writing a lot of times "if let” to unwrap optional values. In particular I do as follows:
var something: Type?
func foo() -> Void {
if let something = something {
doStuff(something) //just an example
}
}
As you can see I usually keep the same variable name and the variable scoping does the rest.
I think it could be a cool syntactic sugar getting the same result as the code above omitting “something =“.
Let me be more clear: if you don’t specify the name of the unwrapped variable, swift does the unwrapping for you, keeping the same variable name.
So the code above would be:
var something: Type?
func foo() -> Void {
if let something {
doStuff(something)
}
}
Let me know what you think and also if you have questions.
Best regards,
Massimo Frasson
iOS Developer, Belka srl
More information about the swift-evolution
mailing list