3 ms·
With generalized pattern matching, your code could (among other things) take the form of case result of Just x -> <do something with x> Not
by wyager 12y ago
With 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") }