因使用“param”而产生的不明确类型变量“a0”阻止了约束“(Parsable a0)”的求解

Ambiguous type variable ‘a0’ arising from a use of ‘param’ prevents the constraint ‘(Parsable a0)’ from being solved

我无法理解此错误消息,不太确定下一步应该调查什么。

我有以下导入:

{-# 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" }

param 的类型为 Parseable a => Text -> ActionM a,这意味着 f 的类型为 a。但是,您永远不会对 f 做任何事情,因此无法推断出 a 应该是什么类型。假设您不想在 do 使用 f 之前只注释掉该行,您需要提供一个明确的类型:

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

您可能会选择具有 Parseable 实例的任何类型,但 L.Text 似乎是 应该 的类型。一旦你真正使用了 f,你就可以删除显式注释。