4 ms·
> Awesome, the ability to unwrap multiple optionals on one line should be bolded, underlined and all caps at the top of this article This is why it's lame that
by wyager 12y ago
> Awesome, the ability to unwrap multiple optionals on one line should be bolded, underlined and all caps at the top of this article
This is why it's lame that Swift's optionals are a language feature and not an in-language implementation.
In Haskell, optionals are Monads, which means there is a function
join :: m (m a) -> m a
"Maybe" (Haskell's standard library Optional) is a monad, so you can unwrap/collapse them using Join.
You can also daisy-chain them together using >>=, a la
fail1 :: Maybe a
fail2 :: a -> Maybe b
result = fail1 >>= fail2
If either fail1 or fail2 fails, the entire "result" function fails (i.e. returns "Nothing").