Hacker News new | ask | show | jobs
by centimeter 2015 days ago
Haskell does a nice job with this as well. There's a lot of machinery available for dealing with error handling, much of it through typeclasses.

    format :: Maybe Int -> String
    format = maybe "No Int Provided" show

    formatIfFormatterAvailable :: Maybe (Int -> String) -> Maybe Int -> Maybe String
    formatIfFormatterAvailable formatter int = formatter <*> int
The latter will work with any error handling type, not just Maybe.

    formatIfAvailable :: (Int -> String) -> Maybe Int -> Maybe String
    formatIfAvailable formatter int = fmap formatter int
And so on. Using a combination of functor, monad, and applicative typeclasses, you can get really ergonomic error handling. It can be a little confusing to see it at first, where during parsing you have expressions like

    data Entry = Entry Username Date Dollars
    parser :: Parser Entry
    parser = Entry <$> usernameParser <*> dateParser <*> dollarsParser
What the above is doing is "first try to parse a username, then try to parse a date, then try to parse a dollar amount, and if they all parse then return an Entry object with all that data". This is a lot easier than writing out something like 3 nested if statements, checking if any of the parsers returned null each time, or trying to use GOTOs or whatever.