解决影响和可能

Fre*_*all 3 purescript

spec = describe "Router" $ do

  let sampleRoutes = [( Tuple "/"  "views/index.yaml" ), 
                      ( Tuple "/foo" "views/foo.yaml" ), 
                      ( Tuple "/bar" "views/bar.yaml" )]

  it "should default to the first of the list" $ do
    r <- fst <$> head sampleRoutes
    fprint r
Run Code Online (Sandbox Code Playgroud)

以上抛出以下错误:

Error in declaration spec
Cannot unify Data.Maybe.Maybe with Control.Monad.Eff.Eff u4505.
Run Code Online (Sandbox Code Playgroud)

我相信这是因为它期望第二个参数是 type Eff,但是由于使用Maybehead第二个参数引入的,最终变成了 type Maybe

it :: forall e a. String -> Eff e a -> Eff (it :: It | e) Unit
Run Code Online (Sandbox Code Playgroud)

问题是,我不知道如何解决这个问题。我不能有一个Maybe有效的代码块吗?

Phi*_*man 5

Maybe可以在do块中使用,但是块中的所有操作都必须是Maybe asome的类型a

这同样适用于Eff eff- 您可以使用Eff effwith do,但所有操作都必须属于Eff eff a某些类型a

您不能在一个do块内混合和匹配两种类型的效果。

它看起来像你想使用类型的值Maybe a一内部do块,其单子是Eff eff。你有几个选择:

  • 使用Data.Array.Unsafe.headwhich 会给你一个 unwrapped Tuple,你可以fst直接调用它。
  • Maybe值的模式匹配来决定Effmonad 中的动作过程:

    it "should default to the first of the list" $ do
      case head sampleRoutes of
        Nothing -> ... -- Handle empty array
        Just tuple -> fprint (fst tuple) -- Print the first component
      .. rest of do block ..
    
    Run Code Online (Sandbox Code Playgroud)