Haskell: Is there any object that is of every type, like `null` in Java?

  • A+

In Java, with a little exception, null is of every type. Is there a corresponding object like that in Haskell?


Short answer: Yes

As in any sensible Turing-complete language, infinite loops can be given any type:

loop :: a loop = loop 

This (well, this, or maybe this) is occasionally useful as a temporary placeholder for as-yet-unimplemented functionality or as a signal to readers that we are in a dead branch for reasons that are too tedious to explain to the compiler. But it is generally not used at all analogously to the way null is typically used in Java code.

Normally to signal lack of a value when that's a sensible thing to do, one instead uses

Nothing :: Maybe a 

which, while it can't be any type at all, can be the lack of any type at all.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: