为Newtype派生'ToHtml`?

时间:2017-09-24 13:17:19

标签: haskell servant

假设:

λ: >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

1 个答案:

答案 0 :(得分:4)

Foo需要派生ToHtml类作为该类的实例。 由于Foonewtype String,因此可以使用以下内容完成:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

我想,这段代码演示了您正在寻找的内容:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

import Lucid.Base

newtype Foo = Foo String deriving ToHtml

main = print $ toHtml (Foo "foo")
相关问题