|
|
|
|
|
by stormbrew
4400 days ago
|
|
Pretty much every language to date allows this construct. And it's often considered a bad idea in languages where = is assignment because it's so easy to get it confused with == and accidentally assign to the thing you're trying to compare to, which will usually evaluate to truthy. The special things about the way it works in Swift are: - You have to put let in front, thus avoiding the confusion. - If the RHS is optional it unwraps the option for the success branch, but the variable is not available in any other scope, thus ensuring safe use. In JS, C, CS, Ruby, etc. you're not really doing anything useful if you assign a value to another name just for one branch of an if statement. In Swift you are. |
|