let someLengthyVariableName: Foo? = 
let anotherImportantVariable: Bar? = 

if let someLengthyVariableName = someLengthyVariableName, let anotherImportantVariable = anotherImportantVariable {
    
}

// or

if let a = someLengthyVariableName, let b = anotherImportantVariable {
    ...
}

optional 변수를 unwrap을 하다 보면 이런 식으로 사용하는 경우가 많다. 단순히 해당 변수가 값을 가지고 있는지 확인하고 싶을 뿐이지만 변수명도 중복 되고, 코드도 깔끔해 보이지 않는다.

(kotlin도 사실 크게 다를 건 없지만 적어도 let과 같은 함수를 쓰면 변수명을 중복으로 쓰지 않아도 된다.)


swift 5.7에서는 이러한 문제를 해결하기 위해 새로운 문법이 도입 되었다. 이를 통해 기존에 같은 변수명으로 설정하여 shadow하는 부분을 간략화 할 수 있다.


let someLengthyVariableName: Foo? = 
let anotherImportantVariable: Bar? = 

if let someLengthyVariableName, let anotherImportantVariable {
    
}
if let foo { ... }
if var foo { ... }

else if let foo { ... }
else if var foo { ... }

guard let foo else { ... }
guard var foo else { ... }

while let foo { ... }
while var foo { ... }

if 뿐만 아니라 다른 구문에서도 사용할 수 있다.

참조)