仿制药的约束类型
我正在尝试将转换为通用类型的YAML。
我实施的简单转换效果很好,但是我想使用他们的show
实例,例如data foo = bar | BIZ
。
我当时想到了像波纹管这样的事情。它正确地捕获了上述类型,但在调用show
时失败,因为我无法正确限制它。
instance {-# OVERLAPPING #-} (GToYaml x, GToYaml y) => GToYaml (D1 d (x :+: y)) where
gToYaml x = string $ pack $ show $ to x
有可能以某种方式限制原始类型具有通用性并显示实例?,还是应该以某种方式处理它?
I'm trying to implement conversion to YAML for Generic types.
The simple conversion I implemented works well, but I would like to use their Show
instance for the types like data Foo = Bar | Biz
.
I was thinking of something like bellow. It correctly catches the types mentioned above, but fails on invoking the show
as I'm not able to properly constraint it.
instance {-# OVERLAPPING #-} (GToYaml x, GToYaml y) => GToYaml (D1 d (x :+: y)) where
gToYaml x = string $ pack $ show $ to x
Is it possible to somehow constraint for original type to have Generic and Show instances? Or it should be handled somehow differently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据推测,原始类型类似于
假设我们定义此辅助数据类型和Typeclass:
HowtoconvertToyAml
被用作datakind
,向toyml'
:是否提供一条额外信息我们是否在特殊情况下。这使我们能够为每种情况定义不同的实例:问题是我们要使用
fotaml
,而不是toyaml'
。我们需要一种方法来自动计算每种类型的howtoconvertToyAml
,然后在toyaml'
上进行toyaml
实例委托。我们可以使用一个类型的家庭:
和
toyaml
根据toyaml'
是:Presumably, the original typeclass is something like
Suppose we define this auxiliary datatype and typeclass:
HowToConvertToYaml
is being used as aDataKind
, to provide one piece of extra information toToYaml'
: whether we are in the special case or not. This allows us to define different instances for each case:The problem is that we want to use
ToYaml
, notToYaml'
. We need a way to automatically compute theHowToConvertToYaml
for each type, and then make theToYaml
instance delegate on theToYaml'
one.We can use a type family:
And
ToYaml
in terms ofToYaml'
would be: