2

GET リクエストを受け取り、それに対する応答としてテキスト メッセージを送信したいと考えています。次のコードがありますが、次のエラーが表示されます。

{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Network.Wai.Middleware.RequestLogger

import Data.Monoid (mconcat) 

main = scotty 4000 $ do
middleware logStdoutDev

get "/" $ do
    beam<- param "q"
    text $ "The message which I want to send"

サーバーを実行しようとすると発生するエラーは

No instance for (Parsable t0) arising from a use of ‘param’
The type variable ‘t0’ is ambiguous
Note: there are several potential instances:
  instance Parsable () -- Defined in ‘scotty-0.9.1:Web.Scotty.Action’
  instance Parsable Bool
    -- Defined in ‘scotty-0.9.1:Web.Scotty.Action’
  instance Parsable Data.ByteString.Lazy.Internal.ByteString
    -- Defined in ‘scotty-0.9.1:Web.Scotty.Action’
  ...plus 9 others
In a stmt of a 'do' block: beam <- param "q"
In the second argument of ‘($)’, namely
  ‘do { beam <- param "q";
        text $ "The message I want to send" }’
In a stmt of a 'do' block:
  get "/"
  $ do { beam <- param "q";
         text $ "The message I want to send" }
4

1 に答える 1

7

paramタイプがありParsable a => Text -> ActionM aます。ご覧のとおり、aはポリモーフィックであり、 のインスタンスのみを必要としますParsable a。こちらもリターンタイプ。しかし、ドキュメントでわかるように、 Parsable にはいくつかのインスタンスが用意されています。GHCはあなたが何を望んでいるのか分からないため、あいまいなエラーになります。必要なものを GHC に知らせる型注釈を指定する必要があります。とにかく、コードを書きましょう

beam <- param "q" :: ActionM Text

トリックを行う必要があります。beamである必要がありますText。しかし、パラメータが整数であることを期待しているかもしれません。

beam <- param "q" :: ActionM Integer

同様に働くことができます。

于 2015-02-13T15:14:20.687 に答える