How to define a variable contained in a tuple?

114 Views Asked by At

Every time I tried to run this code, the compiler returns a 'not in scope' error for the variables redirectUrlGraphEmail, redirectUrlGraphPost, aboutContents, and staticDir:

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

These variables are declared by:

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

but I am not sure where should I add these lines into the module. Any help here?

1

There are 1 best solutions below

3
Guerric Chupin On

I don't know happstack but it seems that your variables redirectUrlGraphEmail etc. are defined in your main (or in some monad at least), right? And then you want to define route as a separate function in your module.

The thing is, the variables you define (that you bind with <- to be precise) in your main are local to your main, so the rest of your program doesn't have access to them (which is why the compiler tells you they're not in scope when you try to use them in routes). If you want to make use of them, you need to define routes as a function taking them as parameters, so in your case:

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

And then from your main call routes with the variables you've bind. Am I making sense?