Kevin Meredith
Kevin Meredith

Reputation: 41909

Using `Maybe` with Data Constructor

I'm working on implementing a todo command-line app in Haskell. Thanks for Learn You a Haskell for the challenge.

In particular, I'm curious about my Action data constructor (supposed to be an enumeration basically) for my Action data type.

data Action = Add | View | Delete      -- 3 options for the tood list

...

execute :: (Maybe Action) -> IO a
execute Just Add    = print "Add not supported"
execute Just View   = view
execute Just Delete = print "Delete not supported"
execute None        = print "invalid user input"

When compiling via ghc --make ..., I get an error:

Not in scope: data constructorNone'`

How can I properly use Maybe Action? Am I incorrectly assuming that Maybe can be attached to any data type instance, i.e. constructor?

Please correct me if I'm using the wrong terminology (data type, constructor, etc).

Upvotes: 1

Views: 453

Answers (1)

Carl
Carl

Reputation: 27003

The specific error you're getting is because the empty constructor for Maybe is Nothing, not None. However, once you fix that you'll get some other baffling error message because you need to parenthesize.

execute :: (Maybe Action) -> IO a
execute (Just Add)    = print "Add not supported"
execute (Just View)   = view
execute (Just Delete) = print "Delete not supported"
execute Nothing       = print "invalid user input"

Otherwise, it would assume that you meant for execute to have two arguments - one for each pattern in its equations.

Upvotes: 6

Related Questions