|
|
|
|
|
by jrockway
5860 days ago
|
|
Most important, though, is the case of the first and most common error. Here, the language will very explicitly require you to handle the case that "Nothing" will potentially be returned. Well, actually no. Consider: divide :: Int -> Int -> Maybe Int
divide x 0 = Nothing
divide x y = Just $ x / y
printResult :: Maybe Int -> IO ()
printResult (Just x) = print x
Now, run: main = do
putStr "Type a number: "
x <- readLn
divide 42 x >>= printResult
This will still die if you type 0, because there is no pattern to match the Nothing case. Sure, you can run Catch to see that you made a mistake, but the compiler doesn't care. (At least you can run Catch to determine that you messed up, though. If you are using Java or Ruby or something, you are just hosed.)Anyway, my usual error is not null or using the wrong variable or typoing something. It's usually failing type checks because I forgot a coercion, or just plain wrong code. Exceptions prevent null returns, and I am lucky with respect to the other two. |
|