Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
1  
@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 '12 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. –  delnan Nov 20 '12 at 19:32
4  
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 '12 at 19:45
2  
@Giorgio How does any of what you just said contradict what I said? –  sepp2k Nov 20 '12 at 19:59
1  
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 '12 at 20:12
show 3 more comments

2 Answers 2

up vote 10 down vote accepted

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"!

share|improve this answer
add comment

Why am I wrong here?

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

share|improve this answer
4  
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 '12 at 19:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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