1

每次我尝试运行此代码时,编译器都会为变量redirectUrlGraphEmailredirectUrlGraphPostaboutContents和返回“不在范围内”错误staticDir

routes :: [ (String, ServerPart Response)]
routes = [
("graph-fb", seeOther redirectUrlGraphEmail $ toResponse ""),
("post-fb", seeOther redirectUrlGraphPost $ toResponse ""),               
("about", aboutResponse aboutContents),
("static", serveDirectory DisableBrowsing [] staticDir)]

这些变量通过以下方式声明:

staticDir <- getStaticDir
redirectUrlGraphEmail <- retrieveAuthURL testUrl
redirectUrlGraphPost <- retrieveAuthURL testPostUrl
aboutContents <- LazyIO.readFile $ markdownPath ++ "README.md"
privacyContents <- LazyIO.readFile $ markdownPath ++ "PRIVACY.md"

但我不确定我应该在哪里将这些行添加到模块中。这里有什么帮助吗?

4

1 回答 1

4

我不知道 happstack 但似乎您的变量redirectUrlGraphEmail等是在您的 main 中定义的(或至少在某些 monad 中),对吗?然后你想route在你的模块中定义一个单独的函数。

问题是,您定义的变量(<-准确地说是绑定的)是您main的本地变量main,因此您的程序的其余部分无法访问它们(这就是编译器告诉您它们不在当您尝试在routes) 中使用它们时的范围。如果要使用它们,则需要定义routes为将它们作为参数的函数,因此在您的情况下:

routes redirectUrlGraphEmail redirectUrlGraphPost = [
 ("graph-fb", seeOther redirectUrlGraphEmail $ toResponse ""),
 ("post-fb", seeOther redirectUrlGraphPost $ toResponse ""),               
 ("about", aboutResponse aboutContents),
 ("static", serveDirectory DisableBrowsing [] staticDir)]

然后从您使用绑定的变量main调用。routes我说得有道理吗?

于 2016-08-04T21:52:37.793 回答