NewtypeDeriving not working for PersistFieldSql

I am trying to detect Markdown newtype and using GeneralizedNewtypeDeriving to automatically detect new instances:

 import Text.Markdown import Yesod.Text.Markdown import Database.Persist.Sql newtype MarkdownNewT = MarkdownNewT { getMarkdown :: Markdown } deriving (Eq, IsString, Monoid, PersistField, PersistFieldSql) 

This does not work for 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' 

Is this due to the new roles GHC 7.8.2 features? In this particular case, I don’t know what to do, since Markdown itself is a new type of text ...

Or is it related to forall on sqlType ? What is the cause of this error when all other instances are successfully received automatically?

thanks

+5
source share
1 answer

This is very similar to some of the examples (in particular, Vector one) on the GHC wiki Roles2 page of things that do not work with the current role system, alas.

The main problem is that in

 class PersistField a => PersistFieldSql a where sqlType :: Monad m => ma -> SqlType 

the monad m can be created using a type constructor whose argument has a nominal role, so m Markdown and m MarkdownNewT not displayed the same way, even if Markdown and MarkdownNewT themselves and the current role system cannot limit m prohibiting such type constructors.

+6
source

Source: https://habr.com/ru/post/1202776/


All Articles