Creating a function that accepts a Void type

I wrote

h :: Void -> Int

I have heard that you can create a function that accepts a Void, although you cannot call it. However, when I try to do so, I get
“Not in scope: type constructor or class Void”

I also tried

h :: Data.Void -> Int

and it said there was no module “Data.”

How can I create a function that accepts a VOid data type?

You need to import Data.Void first:

import Data.Void

h :: Void -> Int
h x = 42
1 Like

Ah, yes… I am of the opinion that Imports are overlooked as a topic for teaching and documentation. This is a good example of that.

1 Like

You can also define your own Void-like type, a datatype without constructors:

{-# LANGUAGE EmptyDataDecls #-}
data MyVoid 

takesVoid :: MyVoid -> a
takesVoid v = case v of -- no constructors, so no cases
1 Like

Good suggestion; you don’t need {-# LANGUAGE EmptyDataDecls #-} since Haskell 2010.

2 Likes