mango
1
I'm following a Swift tutorial book in which the author lists the following code:
“switch number {
case let x where x % 2 == 0:
print("Even")
default:
print("Odd")
}”
Regarding this code, the author then states:
“In the previous example, the binding introduced an unnecessary constant x; it’s simply another name for number.”
Could someone please let me know what the word "binding" means here? I tried searching online to no avail and I'm a bit confused now as to the actual meaning of this word in this context. Please help. Thank you!
jonprescott
(Jonathan Prescott)
2
The "let x = ..." is the binding. You're introducing a local constant called "x" which is bound to the value "number % 2 "(i.e., either 1 or 0), and would only be available in the body of the case block .
jonprescott
(Jonathan Prescott)
3
Actually, I misspoke a bit. "x" is bound to "number" if "number % 2" is = 0. Since you don't use "x" in the case body, it's really unnecessary.
1 Like
young
(rtSwift)
5
1 Like