2013-07-29 62 views
3

我一直在嘗試開始編寫Scotty中的Web應用程序,但是當我嘗試運行服務器時,出現依賴關係衝突。這裏是我的代碼:Haskell依賴衝突

{-# LANGUAGE OverloadedStrings #-} 
module Site where 

import Web.Scotty 
import Control.Monad.IO.Class 
import qualified Data.Text.Lazy.IO as T 

-- Controllers 
indexController :: ActionM() 
indexController = do 
    index <- liftIO $ T.readFile "public/index.html" 
    html index 

routes :: ScottyM() 
routes = do 
    get "/" indexController 

main :: IO() 
main = do 
    scotty 9901 routes 

當我運行它使用runhaskell Site.hs,我得到以下錯誤:

Site.hs:12:10: 
    Couldn't match expected type `text-0.11.2.3:Data.Text.Lazy.Internal.Text' 
       with actual type `Data.Text.Lazy.Internal.Text' 
    In the first argument of `html', namely `index' 
    In a stmt of a 'do' block: html index 
    In the expression: 
     do { index <- liftIO $ T.readFile "public/index.html"; 
      html index } 

使用cabal list text,它告訴我,版本0.11.2.30.11.3.1安裝,但0.11.3.1是默認。 Scotty的scotty.cabal指定text包必須是>= 0.11.2.3,在我看來,上面的代碼應該可以工作。這種錯誤是否有任何解決方法?

回答

5

錯誤消息

Site.hs:12:10: 
    Couldn't match expected type `text-0.11.2.3:Data.Text.Lazy.Internal.Text' 
       with actual type `Data.Text.Lazy.Internal.Text' 

意味着你scotty用的是text包的0.11.2.3版本編譯,但runhaskell的調用選擇使用版本0.11.3.1(因爲這是你有最新的,而你還沒有告訴它使用不同的版本)。就GHC而言,兩種不同軟件包版本的(惰性)Text類型是兩種完全不同的類型,因此,必須使用text的精確版本來編譯scotty庫以運行代碼。

runhaskell -package=text-0.11.2.3 Site.hs 

應該工作。如果你編譯模塊,你還需要告訴GHC直接或通過Cabal使用正確版本的text

另一個選項可能是針對較新的text版本重新編譯scotty

+0

太棒了!有沒有辦法在全球範圍內設置默認包? – nahiluhmot

+0

不,不是。默認情況下使用最新版本(如最高版本號所示)。你可以隱藏你不想使用的版本,'ghc-pkg hide foo-0.1.2.3',然後GHC將不會使用該版本,除非你明確告訴它。所以隱藏所有較新的版本是使某個版本成爲默認版本。 –