簡體   English   中英

嘗試解除 Aff 響應時沒有類型類實例錯誤

[英]No type class instance error when trying to lift Aff response

我是 Purescript 的新手,正在嘗試學習 Halogen/Aff。 我一直在開發一個簡單的應用程序,它是 purescript-halogen 存儲庫中 effects-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。 我需要讓組件使用 MonadAff 而不是 MonanEffect。 謝謝!

component :: ∀ f i o m. MonadAff m => H.Component HH.HTML f i o m

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM