1

我有点被这个错误信息难住了

Warning: Error in module Chapter2:
Error in value declaration unionIsForeign:
Error checking that type
  Control.Monad.Eff.Eff (trace :: Debug.Trace.Trace | u8717) u8715
subsumes type
  Data.Either.Either Data.Foreign.ForeignError Chapter2.Union
Error at src/Chapter2.purs line 16, column 18 - line 20, column 1:
  Cannot unify type
    Control.Monad.Eff.Eff
  with type
    Data.Either.Either

这是这段代码的结果

module Chapter2 where

import Debug.Trace
import Data.Foreign
import Data.Foreign.Class
import Control.Monad.Eff

sideeffect :: forall e. Number -> Eff (trace :: Trace | e) Number
sideeffect v = return v

data Union = S String 

instance unionIsForeign :: IsForeign Union where
    read value = do
        v <- sideeffect 42
        return $ S "Test"

main = do
    trace "Test"

为什么需要统一EffEither?这似乎只在实例化 IsForeign 类型类时出现。 sideeffect运行愉快main- 当然 main 有一个签名,说明它使用 Eff Monad

> :t Chapter2.main
Compiling Chapter2
forall t20. Control.Monad.Eff.Eff (trace :: Debug.Trace.Trace | t20) Prelude.Unit
4

1 回答 1

1

关键部分是:

来自Data.Foreign.Class

class IsForeign a where
  read :: Foreign -> F a

并从Data.Foreign

type F = Either ForeignError

因此,您的read函数具有声明的返回类型Either ForeignError Union.

但是由于您使用sideeffectwhich 的返回类型为Eff (trace :: Trace | e) Number,因此在 do 块中,do 块的推断类型是一个Eff ...值。

也就是说,您试图从必须返回 anEff ...的东西中返回 anEither ...并且编译器正在抱怨,因为它无法Eff ...Either ....

于 2015-05-26T13:03:54.687 回答