There's a very common problem that is very easy to get into with haskell. Code-snippet that describes it is this:
data JobDescription = JobOne { n :: Int }
| JobTwo
| JobThree { n :: Int }
deriving (Show, Eq)
taskOneWorker :: JobDescription -> IO ()
taskOneWorker t = do
putStrLn $ "n: " ++ (show $ n t)
main :: IO ()
main = do
-- this runs ok:
taskOneWorker (JobOne 10)
-- this fails at runtime:
-- taskOneWorker JobTwo
-- this works, but we didn't want it to:
-- taskOneWorker (JobThree 10)
I described this problem and it's possible solutions in this article: https://www.fpcomplete.com/user/k_bx/playing-with-datakinds
Here, on StackOverflow, I'd like to ask you -- what are the best solutions to this problem, using which tools and what documentation?
nOne :: JobDescription -> Int
that only works forJobOne
s.taskOneWorker JobTwo
to crash, so I just copied the behaviour. If you don't want it to crash you should usePrism
s.