gpt4 book ai didi

haskell - 输入别名和 “instance … where”

转载 作者:行者123 更新时间:2023-12-02 10:44:05 24 4
gpt4 key购买 nike

我有一个类型别名:

type MyId = UUID

和数据类型:
data MyData = MyData { myId :: MyId, .... }

然后我为 FromJSONToJSON创建实例,为 MyData
instance FromJSON MyData where
parseJSON (Object v) = ....

instance ToJSON MyData where
toJSON (Object v) = ....

我想知道为什么会有错误:
1) No instance for (`FromJSON MyId`) arising from a use of ‘.:’
2) No instance for (ToJSON MyId) arising from a use of ‘.=’
instance FromJSONToJSONUUID已经实现, MyId是否自动拥有它们?如果没有,我将如何实现它们?
instance FromJSON MyId where
parseJSON (Object v) = --- ? It's already UUID for which FromJSON is implemented

instance ToJSON MyId where
toJSON (Object v) = --- ? It's already UUID for which FromJSON is implemented

最佳答案

您要从Uuid-aeson包导入Data.UUID.Aeson吗?

这对我有用:

import Data.UUID
import Data.UUID.Aeson
import Data.Aeson

type MyUUID = UUID

data U2 = U2 MyUUID Int

instance ToJSON U2 where
toJSON(U2 a b) = toJSON a

关于haskell - 输入别名和 “instance … where”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31295473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com