我希望在这段代码中编写 isShowable
函数。
data MaybeShowable = forall a . Show a => Showable a | Opaque
f :: (Data d) => d -> String
f x = case isShowable x of
Showable s -> show s
Opaque -> "<<OPAQUE>>"
isShowable :: (Data d) => d -> MaybeShowable
isShowable = ???
通过使用Data
实例,这是否可能实现?如果不行,最好的方式是什么?
注意:如果没有其他选项,我愿意接受仅适用于在定义isShowable
的模块中可见的类型类实例的版本。
Data.Typeable
或Data.Data
API中,这不是其中的一部分。 - bheklilrisShowable
的模块中进行查看”,这会改变你的答案吗? @bheklilr @chi - tohavaIncoherentInstances
或相关扩展。尽管我不是那个领域的专家,无法给出明确的答案。 - chi