GeneralizedNewtypeDeriving对于PersistFieldSql失败 [英] GeneralizedNewtypeDeriving fails for PersistFieldSql
问题描述
我正在尝试定义 Markdown 新类型,并且使用GeneralizedNewtypeDeriving
自动定义新实例:
I'm trying to define a Markdown newtype, and using GeneralizedNewtypeDeriving
to automatically define new instances:
import Text.Markdown
import Yesod.Text.Markdown
import Database.Persist.Sql
newtype MarkdownNewT = MarkdownNewT { getMarkdown :: Markdown }
deriving (Eq, IsString, Monoid, PersistField, PersistFieldSql)
对于PersistFieldSql
,它失败,并显示以下消息:
This fails for the PersistFieldSql
with the following message:
Could not coerce from ‘m Markdown’ to ‘m MarkdownNewT’
because ‘m Markdown’ and ‘m MarkdownNewT’ are different types.
arising from the coercion of the method ‘sqlType’ from type
‘forall (m :: * -> *). Monad m => m Markdown -> SqlType’ to type
‘forall (m :: * -> *). Monad m => m MarkdownNewT -> SqlType’
这是由于GHC 7.8的新角色功能引起的.2?在那种特殊情况下,我不知道该怎么办,因为Markdown本身就是Text上的新类型...
Is this due to the new roles features of GHC 7.8.2? In that particular case I don't know what to do, since Markdown is itself a newtype over Text...
这是否与sqlType
上的forall
有关?成功成功自动导出所有其他实例时,此错误的原因是什么?
Or is this related with the forall
on sqlType
? What is the reason for this error when all other instances are successfully automatically derived?
谢谢
推荐答案
这看起来与GHC Wiki Roles2 页面无法使用当前角色系统,,.
This looks very similar to some of the examples (in particular the Vector
one) in the GHC wiki Roles2 page of things that don't work with the current role system, alas.
基本上,问题在于
class PersistField a => PersistFieldSql a where
sqlType :: Monad m => m a -> SqlType
monad m
可能会使用类型构造函数实例化,该构造函数的参数具有名义作用,因此即使Markdown
和m MarkdownNewT
不会表示相同>本身就是-并且当前的角色系统无法限制m
以禁止此类类型构造函数.
the monad m
might be instantiated with a type constructor whose argument has nominal role, so that m Markdown
and m MarkdownNewT
aren't identically represented even if Markdown
and MarkdownNewT
themselves are - and the current role system has no way of restricting m
to disallow such type constructors.
这篇关于GeneralizedNewtypeDeriving对于PersistFieldSql失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!