Null object: Difference between revisions

Content added Content deleted
(add Haskell example)
Line 24: Line 24:
}</cpp>
}</cpp>


=={{header|Haskell}}==

Haskell does not have a universal null value. There is a 'value of every type', the undefined value (sometimes written ⊥, 'bottom'), but it is essentially a sort of exception — any attempt to use it is an error.

undefined -- undefined value provided by the standard library
error "oops" -- another undefined value
head [] -- undefined, you can't take the head of an empty list

When one would use "null" as a marker for "there is no normal value here" (e.g. a field which is either an integer or null), one uses the Maybe type instead. The definition of Maybe is:

data Maybe a = Nothing | Just a

That is, a <code>Maybe Integer</code> is either <code>Nothing</code> or <code>Just </code>&lt;some integer&gt;.

There are many ways to work with Maybe, but here's a basic case expression:

case thing of
Nothing -> "It's Nothing. Or null, whatever."
Just v -> "It's not Nothing; it is " ++ show v ++ "."
=={{header|Java}}==
=={{header|Java}}==
<java>if(object == null){
<java>if(object == null){