我正在关注Snap示例项目,但是当我复制示例代码时,Stack给了我错误抱怨使用writeBS
以及它如何期望ByteString
但是给了[Char]
1}}。所以我导入了一个ByteString库并尝试使用'fromString'方法将所有字符串转换为ByteStrings,但Stack给了我同样的错误,声称Srings仍然是[Char]
。这是经过修改的示例代码:
module Main where
import Snap
import Snap.Types
import Snap.Util.FileServe
import Control.Applicative
import Heist
import qualified Data.ByteString.UTF8 as BU
main :: IO ()
main = quickHttpServe site
site :: Snap ()
site =
ifTop (writeBS $ BU.fromString "hello world") <|>
route [ ("foo", writeBS $ BU.fromString "bar")
, ("echo/:echoparam", echoHandler)
] <|>
dir "static" (serveDirectory ".")
echoHandler :: Snap ()
echoHandler = do
param <- getParam "echoparam"
maybe (writeBS $ BU.fromString "must specify echo/param in URL")
writeBS $ BU.fromString param
尝试使用fromString
进行修复后出现错误消息:
/home/jeshaitan/Personal/yapp/src/Main.hs:16:14:
Couldn't match expected type ‘BU.ByteString’
with actual type ‘[Char]’
In the expression: "foo"
In the expression: ("foo", writeBS $ BU.fromString "bar")
In the first argument of ‘route’, namely
‘[("foo", writeBS $ BU.fromString "bar"),
("echo/:echoparam", echoHandler)]’
/home/jeshaitan/Personal/yapp/src/Main.hs:17:14:
Couldn't match expected type ‘BU.ByteString’
with actual type ‘[Char]’
In the expression: "echo/:echoparam"
In the expression: ("echo/:echoparam", echoHandler)
In the first argument of ‘route’, namely
‘[("foo", writeBS $ BU.fromString "bar"),
("echo/:echoparam", echoHandler)]’
/home/jeshaitan/Personal/yapp/src/Main.hs:19:9:
Couldn't match expected type ‘BU.ByteString’
with actual type ‘[Char]’
In the first argument of ‘dir’, namely ‘"static"’
In the second argument of ‘(<|>)’, namely
‘dir "static" (serveDirectory ".")’
In the expression:
ifTop (writeBS $ BU.fromString "hello world")
<|>
route
[("foo", writeBS $ BU.fromString "bar"),
("echo/:echoparam", echoHandler)]
<|> dir "static" (serveDirectory ".")
/home/jeshaitan/Personal/yapp/src/Main.hs:23:23:
Couldn't match expected type ‘BU.ByteString’
with actual type ‘[Char]’
In the first argument of ‘getParam’, namely ‘"echoparam"’
In a stmt of a 'do' block: param <- getParam "echoparam"
In the expression:
do { param <- getParam "echoparam";
maybe
(writeBS $ BU.fromString "must specify echo/param in URL") writeBS
$ BU.fromString param }
/home/jeshaitan/Personal/yapp/src/Main.hs:25:21:
Couldn't match expected type ‘Maybe BU.ByteString’
with actual type ‘BU.ByteString’
In the second argument of ‘($)’, namely ‘BU.fromString param’
In a stmt of a 'do' block:
maybe
(writeBS $ BU.fromString "must specify echo/param in URL") writeBS
$ BU.fromString param
/home/jeshaitan/Personal/yapp/src/Main.hs:25:35:
Couldn't match type ‘Maybe BU.ByteString’ with ‘[Char]’
Expected type: String
Actual type: Maybe BU.ByteString
In the first argument of ‘BU.fromString’, namely ‘param’
In the second argument of ‘($)’, namely ‘BU.fromString param’
-- While building package yapp-0.1.0.0 using:
/home/jeshaitan/.stack/setup-exe-cache/x86_64-linux/setup-Simple-Cabal-1.22.4.0-ghc-7.10.2 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.22.4.0 build exe:yapp --ghc-options " -ddump-hi -ddump-to-file"
Process exited with code: ExitFailure 1
答案 0 :(得分:4)
我需要将{-# LANGUAGE OverloadedStrings #-}
添加到文件的顶部,以便字符串文字可以读作ByteStrings。