假设:
λ: >import Servant.HTML.Lucid
我创建了newtype
:
λ: >newtype Foo = Foo String
但是,范围内似乎没有ToHtml Foo
类型类:
λ: >instance ToHtml Foo
<interactive>:3:10: warning: [-Wmissing-methods]
• No explicit implementation for
‘toHtml’ and ‘toHtmlRaw’
• In the instance declaration for ‘ToHtml Foo’
String
还存在一个:
λ: >:t toHtml
toHtml :: (Monad m, ToHtml a) => a -> HtmlT m ()
λ: >toHtml "foo"
foo
如果没有明确的实例定义,我如何获得ToHtml Foo
?
答案 0 :(得分:4)
Foo
需要派生ToHtml
类作为该类的实例。
由于Foo
是newtype
String
,因此可以使用以下内容完成:
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
我想,这段代码演示了您正在寻找的内容:
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Lucid.Base
newtype Foo = Foo String deriving ToHtml
main = print $ toHtml (Foo "foo")