#30: Variable Shadowing
Question
// Swift Quiz
— objc.io (@objcio) September 11, 2019
func test(x: () -> ()) {
func x() {
print("Function")
}
x()
}
test {
print("Block")
}
// What's the output?
Solution
Solution: Block
— objc.io (@objcio) September 12, 2019
This quiz shows why variable shadowing is usually a bad idea: either "Block" or "Function" is a reasonable answer.
Ideally, the compiler would give a warning when this happens.https://t.co/s7500CWmAg