4

I'm new to Haskell and I'm not really sold on the Nothing constructor. It sounds like just another silent fail that's going to create headaches down the line. It seems much more useful to throw an error so I'll have a clue what's happening.

Why am I wrong here?

8
  • 2
    @Hex Bob-omb: You throw an error when you encounter one. Nothing indicates one possible result of a partial function. Nothing does not indicate an error but the correct result of a computation.
    – Giorgio
    Nov 20, 2012 at 19:30
  • 1
    @HexBob-omb programmers.stackexchange.com/search?q=maybe and stackoverflow.com/questions/tagged/maybe for starters. There are probably other searches that yield even more.
    – user7043
    Nov 20, 2012 at 19:32
  • 5
    I wouldn't really say that Nothing is a silent fail. Unlike null in other languages, functions that may return Nothing will say so in their type and the type system will force you to handle the possibility of Nothing in some way.
    – sepp2k
    Nov 20, 2012 at 19:45
  • 2
    @Giorgio How does any of what you just said contradict what I said?
    – sepp2k
    Nov 20, 2012 at 19:59
  • 2
    I agree with sepp2k. Just to add a few ideas: Languages that support option types also support exceptions to signal real errors. Nothing indicates the successful computation of a result. Example: if you look up a customer in a database you return Nothing if you do not find the customer, you throw an exception if there is not database connection.
    – Giorgio
    Nov 20, 2012 at 20:12

2 Answers 2

15

In addition to Telastyn's answer:

Maybe never "silently fails". In contrast to null, which might be what the OP's is comparing it to, a Haskell function which can return Nothing must explicitly do so in its type.

For comparison: a method returning String in Java might return a String or null, and you cannot tell just by looking at its type:

public String myFunc(int x) { /* do something, might return null! */ }

In Haskell a function which returns a String has a type similar to this:

myFunc :: Int -> String

You know it cannot return Nothing, because if it did, its type would be:

myFunc :: Int -> Maybe String

This means Nothing can never sneak up on you and "cause headaches down the line"!

13

Why am I wrong here?

Because a lack of value is not always indicative of an error.

1
  • 7
    Exactly (+1). Error means: I do not know how to continue the computation. Nothing means: I have successfully completed the computation and determined that there is no result for the given inputs.
    – Giorgio
    Nov 20, 2012 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.