3 ms·
I'm not a language guy so I don't know anything about generalized pattern matching. Maybe it is better. But for my own use, "if let" ensures that I always take
by archagon 12y ago
I'm not a language guy so I don't know anything about generalized pattern matching. Maybe it is better. But for my own use, "if let" ensures that I always take care of any possible nulls in my code, while also keeping the conditional code in the same scope as the verified variable. There's no easy way to leave pointers dangling or uninitialized. The language design forces me to organize and think about my code in a much better way.
- wyager 12y agoWith generalized pattern matching, your code could (among other things) take the form of case result of Just x -> <do something with x> Nothing -> <do something without x> This works with optionals as well as any other algebraic data type.
- archagon 12y agoI'm not sure if this is the case in your example, but at least in Swift, the "if let" block or chaining for optionals is mandatory. This should mean that it's impossible to have a dangling/null pointer unless you use the force dereference operator. (And that's something that should almost never appear in your code!) You don't get the choice of being lazy or forgetting about checking your pointers: it's a conscious decision either way. Personally, I appreciate that the language naturally makes me organize my code better and more safely.
- masklinn 12y agoIt is, and you can pattern-match Optional in Swift, something along the lines of switch optional { case .Some(let numeral): println("Caught a \(numeral)") default: println("Nothing to catch") }