2015-12-25 2 views
0

Я следую примеру проекта Snap, но когда я скопировал код примера, Stack дал мне ошибки, жалующиеся на использование writeBS и как он ожидает ByteString, но ему дается [Char]. Поэтому я импортировал библиотеку ByteString и пытался преобразовать все строки в ByteStrings с помощью метода fromString, но Stack дает мне ту же ошибку, утверждая, что Srings все еще [Char]. Вот лечил пример кода:Почему в примере Snap используется Bytestring?

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 
+1

Пожалуйста, введите ошибку, которую вы получаете от GHC. – hao

+0

@HaoLian Я думаю, что сообщения об ошибках в основном просто суммируются как «' [Char] 'не соответствует ожидаемому' ByteString' ' – jeshaitan

+2

Если вы пишете '{- # LANGUAGE OverloadedStrings # -}' в верхней части файла , строковые литералы могут быть прочитаны как bytestrings, и похоже, что большинство этих ошибок исчезнет. Как бы то ни было, компилятор может понимать их только как «[Char]». – Michael

ответ

4

мне нужно добавить {-# LANGUAGE OverloadedStrings #-} в верхней части файла, так что строковые литералы можно прочитать как байтовых строк ,

Смежные вопросы