0

I'm following the Snap example project, but when I copied the example code, Stack gave me errors complaining about the use of writeBS and how it expects a ByteString but is given a [Char]. So I imported a ByteString library and tried to convert all of the strings to ByteStrings using the 'fromString' method, but Stack is giving me the same error, claiming that the Srings are still [Char]. Here's the doctored example code:

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

Error Message after trying to fix with 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
jeshaitan
  • 301
  • 1
  • 4
  • 16
  • 1
    Please paste in the error you get from GHC. – hao Dec 25 '15 at 16:30
  • @HaoLian I think that the error messages are basically just summed up as "`[Char]` doesn't match expected `ByteString`" – jeshaitan Dec 25 '15 at 17:20
  • 2
    If you write `{-# LANGUAGE OverloadedStrings #-}` at the top of the file, the string literals can be read as bytestrings, and it looks like most of these errors will go away. As it is, the compiler can only understand them as `[Char]`. – Michael Dec 25 '15 at 17:30
  • Thanks! I just figured that out and was about to close the question, but thanks still! – jeshaitan Dec 25 '15 at 17:38

1 Answers1

4

I needed to add {-# LANGUAGE OverloadedStrings #-} to the top of the file, so that string literals can be read as ByteStrings.

jeshaitan
  • 301
  • 1
  • 4
  • 16