Purescript:为什么genericEncodeJSON无法在容器内编码类型?

时间:2018-08-24 19:33:24

标签: purescript

我不明白为什么下面的程序无法编译。

module Main where

import Prelude
import Effect (Effect)
import Effect.Console (log)
import Data.Generic.Rep (class Generic)
import Foreign.Generic (defaultOptions, genericEncodeJSON)

main :: Effect Unit
main = do
  log "Hello sailor!"

newtype Foo = Foo {
    x :: Int
  , y :: Int
  , z :: Array Bar
}

type Bar = {
    a :: String
  , b :: String
}

derive instance repGenericFoo:: Generic Foo _

fooToJSON :: Foo -> String
fooToJSON = genericEncodeJSON $ defaultOptions { unwrapSingleConstructors = true }

这是编译器的输出

Compiling Main
Error found:
in module Main
at src/Main.purs line 27, column 13 - line 27, column 30

  No type class instance was found for

    Foreign.Class.Encode { a :: String
                         , b :: String
                        }

如果不使用Array,则程序会编译

newtype Foo = Foo {
    x :: Int
  , y :: Int
  , z :: Bar
}

type Bar = {
    a :: String
  , b :: String
}

如果BarBar位于数组或列表之类的容器中,为什么GenericEncodeJSON无法对Bar进行编码?

0 个答案:

没有答案