I'm approaching Haskell with a view of converting runtime errors to compile-time errors. I expect the compiler to figure out that all the code paths in the following code are not error free. If authorizeUser
returns UnauthorizedCommand
then the call to (command $ authorizeUser cmd userId)
will fail during runtime.
data Command = UnauthorizedCommand | Command {command :: String, userId :: String}
authorizedUsers = ["1", "2", "3"]
authorizeUser :: String -> String -> Command
authorizeUser cmd userId = if (userId `elem` authorizedUsers) then Command {command=cmd, userId=userId} else UnauthorizedCommand
main :: IO ()
main = do
cmd <- getLine
userId <- getLine
case (command $ authorizeUser cmd userId) of
"ls" -> putStrLn "works"
_ -> putStrLn "not authorized"
Is there a compiler switch to enable such checking?