【发布时间】:2020-06-12 19:03:17
【问题描述】:
在下面的代码中我收到警告Orphan instance: instance (MonadIO m, Monad m) => GenerateUUID m
instance (MonadIO m, Monad m) => GenerateUUID m where
generateUUID = liftIO nextRandom
根据它的解决方案是
move the instance declaration to the module of the class or of the type, or
wrap the type with a newtype and declare the instance on the new type.
(或禁用警告帽子互联网也建议)
我的问题是我找不到如何用新类型包装类型?
【问题讨论】:
-
看看 Learn You A Haskell for Great Good,了解有关 Haskell 的一些基础知识。
-
GenerateUUID 是在哪里定义的?
-
我很确定你不应该像这样定义一个实例。如果您确实定义了一个实例
GenerateUUID m,那么这将最终成为 any 类型m的实例,因此您将无法再定义任何GenerateUUID实例,因为它们将与您的最大通用m实例重叠。 (由于 GHC 的实例解析是如何工作的,即使您给出约束(MonadIO m, Monad m)也是如此。) -
看来
generateUUID应该是顶级定义,而不是类型类的方法。您是否创建了这个GenerateUUID类型类?还是来自图书馆?
标签: haskell ghc deriving derivingvia