2016-09-07 61 views
1

我無法理解這個錯誤消息,不太確定接下來應該調查什麼。由於使用'param'而產生的模糊類型變量'a0'阻止了'約束'(Parsable a0)'的解決

我有以下的進口:

{-# LANGUAGE OverloadedStrings #-} 
module Main where 

import Web.Scotty 

import Control.Applicative 
import Control.Monad.IO.Class 
import Database.SQLite.Simple 
import Database.SQLite.Simple.FromRow 
import qualified Data.Text.Lazy as L 

的代碼導致錯誤:

routes :: ScottyM() 
routes = do 
    post "/create" $ do 
    f <- param ("fa" :: L.Text) 
    file "create.html" 

和錯誤:

• Ambiguous type variable ‘a0’ arising from a use of ‘param’ 
    prevents the constraint ‘(Parsable a0)’ from being solved. 
    Probable fix: use a type annotation to specify what ‘a0’ should be. 
    These potential instances exist: 
    instance Parsable Integer 
     -- Defined in ‘scotty-0.11.0:Web.Scotty.Action’ 
    instance Parsable L.Text 
     -- Defined in ‘scotty-0.11.0:Web.Scotty.Action’ 
    instance Parsable() 
     -- Defined in ‘scotty-0.11.0:Web.Scotty.Action’ 
    ...plus 7 others 
    ...plus 12 instances involving out-of-scope types 
    (use -fprint-potential-instances to see them all) 
• In a stmt of a 'do' block: f <- param ("f" :: L.Text) 
    In the second argument of ‘($)’, namely 
    ‘do { f <- param ("f" :: L.Text); 
      file "create.html" }’ 
    In a stmt of a 'do' block: 
    post "/create" 
    $ do { f <- param ("f" :: L.Text); 
      file "create.html" } 

回答

4

param的類型爲Parseable a => Text -> ActionM a,這意味着f將有類型a。然而,你從來沒有做任何事情f,所以沒有辦法推斷什麼類型a應該是。假設你不想只是註釋行,直到你使用f,您需要提供一個明確的類型:

routes :: ScottyM() 
routes = do 
    post "/create" $ do 
    f <- param ("fa" :: L.Text) :: ActionM L.Text 
    file "create.html" 

你也許可以挑選具有Parseable實例的任何類型,但L.Text似乎就像應該是那樣。一旦你真的使用f,你可能會刪除明確的註釋。

相關問題