Pattern variable binding cannot appear in an expression

swift

Solution

A "pattern variable binding" is the thing you're doing, i.e. using `let` in the middle of some code, not at the top level of a file or enum or struct or class as a way of declaring a constant variable.

What makes it an expression is the parentheses. You've cut the "let" expression off from its surroundings and asked for evaluation of it as an expression separately. But you can't do that: you can't say "let" just anywhere.

Another way of looking at it is simply this: `if let` is a fixed meaningful pattern, where the condition is an Optional being evaluated into a constant for use inside the if-code. The parenthesis broke up the pattern.

The pattern is called a binding because you're defining this name very temporarily and locally, i.e. solely down into the if-code. I think it goes back to LISP (at least, that's where I've used "let" this way in the past).

Problem

I've been looking at The Swift Programming Language guide provided by apple. Following sample is from the book: ``` class HTMLElement { let name :String; let text: String?; @lazy var asHTML : () -> String = { if let text = self.text { return "<\(self.name)>\(self.text)</\(self.name)>"; } else { return "<\(self.name) />" } } } ``` I incorrectly wrote the closure as follow: ``` @lazy var asHTML : () -> String = { if (let text = self.text) { return "<\(self.name)>\(self.text)</\(self.name)>"; } else { return "<\(self.name) />" } } ``` Notice the parentheses around `let text = self.text` and compiler complain about: Pattern variable binding cannot appear in an expression Just wondering what does `Pattern Variable Binding` mean, and why it cannot appear in an expression?

Original source