我正在接近Haskell,将运行时错误转换为编译时错误.我希望编译器能够确定以下代码中的所有代码路径都没有错误.如果authorizeUser返回UnauthorizedCommand则调用(command $ authorizeUser cmd userId)将在运行时失败.
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"
Run Code Online (Sandbox Code Playgroud)
是否有编译器开关来启用此类检查?
我希望我的应用程序的客户端ID和密码ID不属于我的源代码.因此,如果我在运行时从环境变量中读取这些设置,它们将始终包含在IO中.这会导致问题,因为他们将无法撰写:
authPlugins :: master -> [AuthPlugin master]
authGoogleEmail :: YesodAuth m => Text -> Text -> AuthPlugin m
getEnv :: String -> IO String
Run Code Online (Sandbox Code Playgroud)
提出这个问题的另一种方法是:如何在http://www.yesodweb.com/book/authentication-and-authorization中给出的第一个示例代码中读取clientId和clientSecret从环境中读取
我正在尝试编写一个通用记录更新程序,它允许用户轻松更新existing记录中的字段,字段在类似形状的incoming记录中.这就是我现在所拥有的:
applyUpdater fields existing incoming =
let getters = DL.map (^.) fields
setters = DL.map set fields
updaters = DL.zipWith (,) getters setters
in DL.foldl' (\updated (getter, setter) -> setter (getter incoming) updated) existing updaters
Run Code Online (Sandbox Code Playgroud)
我希望以下列方式使用它:
applyUpdater
[email, notificationEnabled] -- the fields to be copied from incoming => existing (this obviously assumed that `name` and `email` lenses have already been setup
User{name="saurabh", email="blah@blah.com", notificationEnabled=True}
User{name="saurabh", email="foo@bar.com", notificationEnabled=False}
Run Code Online (Sandbox Code Playgroud)
这不起作用,可能是因为Haskell推断出一个非常奇怪的类型签名,applyUpdater这意味着它没有做我期望它做的事情:
applyUpdater :: [ASetter t1 t1 a …Run Code Online (Sandbox Code Playgroud) 是否有任何命令行开关可以stack告诉它下载所有相关软件包,而无需编译/安装任何内容?