4 ms·
Swift has a built in option type “if let actualNumber = possibleNumber.toInt() { println("\(possibleNumber) value: \ (actualNumber)") } else { prin
by jaxytee 12y ago
Swift has a built in option type
“if let actualNumber = possibleNumber.toInt() {
println("\(possibleNumber) value: \ (actualNumber)")
} else {
println("\(possibleNumber) could not be converted to an integer")
}”
Nice to see the Apple's language developers embracing functional programming by providing a clean implementation of the Maybe Monad as well as support for closures.
- nardi 12y agoI love this feature, but it's not quite the Maybe Monad, which lets you assemble a sequence of (possibly failing) statements together, and fail the whole sequence if any of the parts fails (without if-statements or switching of any kind).
- anon1385 12y ago'Optional Chaining' sounds like it lets you join thing together to some extent: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html https://developer.apple.com/library/prerelease/ios/documenta...
- nardi 12y agoOh, cool! Straight out of CoffeeScript. Yeah, that's pretty much the monad instance of Maybe.
- a-nikolaev 12y agoI don't think this is very functional. First of all, the let binding inside the condition has type bool by itself (instead of the normal let-in construction). Also, it's scope permeates into the body of the if condition. The whole thing is very much like C-style NULL/Not-NULL check: if (x = proc()) {printf("%i", x);}