尝试提升Aff响应时没有类型类实例错误

问题描述

我是PureScript的新手,正在尝试学习Halogen / Aff。我一直在开发一个简单的应用程序,该应用程序是纯脚本卤素回购中的effect-aff-ajax示例的变体。我几乎一切正常,除了下面的错误

发生错误代码部分与effects-aff-ajax示例中的代码非常相似(我尚未编译,但只能假设可行)。检查代码,我看不出为什么它不应该引起Aff响应。

Error found:
in module Component
at src/purs/Component.purs:59:17 - 59:26 (line 59,column 17 - line 59,column 26)

  No type class instance was found for
                                
    Effect.Aff.Class.MonadAff m2
                                

while checking that type forall m. MonadAff m => (forall a. Aff a -> m a)
  is at least as general as type t0 -> t1
while checking that expression liftAff
  has type t0 -> t1
in value declaration handleAction

where m2 is a rigid type variable
        bound at (line 54,column 16 - line 60,column 60)
      t1 is an unkNown type
      t0 is an unkNown type

这是完整的模块:

module Component (component) where

import Prelude (Unit,bind,discard,map,($),(<<<),(<>))

import Affjax as AX
import Affjax.RequestBody as AXRB
import Affjax.ResponseFormat as AXRF
import Data.Either (hush)
import Data.Maybe (Maybe(..))
import Effect.Class (class MonadEffect)
import Halogen as H
import Halogen.HTML as HH
import Halogen.HTML.Events as HE
import Halogen.HTML.Properties as HP
import Web.Event.Event as E

type State = { source :: String,translation :: Maybe String }

data Action = InputSource String 
  | TranslateSource State E.Event

component :: ∀ f i o m. MonadEffect m => H.Component HH.HTML f i o m
component =
  H.mkComponent
    { initialState,render,eval: H.mkEval $ H.defaultEval { handleAction = handleAction }
    }

initialState :: ∀ i. i -> State
initialState _ = { source: "that wine is very delicIoUs",translation: nothing }

render :: ∀ m. State -> H.ComponentHTML Action () m
render state =
  HH.form
    [ HE.onSubmit (Just <<< TranslateSource state) ]
    [ HH.h1_ [ HH.text "English to Italian Translation" ],HH.input
        [ HP.type_ HP.InputText,HP.value state.source,HE.onValueInput $ Just <<< InputSource
        ],HH.p_ [],HH.button
        [ HP.type_ HP.ButtonSubmit ]
        [ HH.text "Translate" ],HH.p_ [ HH.text $ case state.translation of
                          nothing -> ""
                          Just t -> "Translation: " <> t 
            ]         
    ]

handleAction :: ∀ o m. MonadEffect m => Action -> H.HalogenM State Action () o m Unit
handleAction = case _ of
  InputSource s -> 
    H.modify_ _{ source = s }
  TranslateSource st ev -> do
    H.liftEffect $ E.preventDefault ev
    response <- H.liftAff $ AX.post AXRF.string "http://locahost:8080/translate" (Just $ AXRB.string st.source)
    H.modify_ _{ translation = map _.body (hush response) }

解决方法

是的,就是费奥多·索金(Fyodor Soikin)。我需要使Component使用MonadAff而不是MonanEffect。谢谢!

transient protected Function<SerField,String> lambda;