【问题标题】:Using Lucid for Simple Example使用 Lucid 做简单的例子
【发布时间】:2017-11-14 13:21:33
【问题描述】:

给定:

import Lucid
import Lucid.Base

mainPage :: Html ()
mainPage = div_ (p_ "hello")

我收到以下编译时错误:

/Users/kevinmeredith/Workspace/my-project/src/Lib.hs:9:18: error:
    • Couldn't match type ‘HtmlT Data.Functor.Identity.Identity ()’
                     with ‘[Char]’
        arising from a functional dependency between:
          constraint ‘Term [Char] (HtmlT Data.Functor.Identity.Identity ())’
            arising from a use of ‘p_’
          instance ‘Term (HtmlT m a) (HtmlT m a)’ at <no location info>
    • In the first argument of ‘div_’, namely ‘(p_ "hello")’
      In the expression: div_ (p_ "hello")
      In an equation for ‘mainPage’: mainPage = div_ (p_ "hello")

请问我该如何解决这个编译时错误?

【问题讨论】:

    标签: html haskell haskell-lucid


    【解决方案1】:

    正如documentation中所写:

    简介

    (..)

    对于GHCi

    :set -XOverloadedStrings -XExtendedDefaultRules@
    import Lucid
    

    在一个模块中:{-# LANGUAGE OverloadedStrings, ExtendedDefaultRules #-}

    (..)

    所以你需要打开OverloadedStringsExtendedDefaultRules扩展。

    您可以通过编译来做到这一点:

    ghc <b>-XOverloadedStrings -XExtendedDefaultRules</b> file.hs

    但也许更方便的是在文件头打开扩展

    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE ExtendedDefaultRules #-}
    
    import Lucid
    import Lucid.Base
    
    mainPage :: Html ()
    mainPage = div_ (p_ "hello")

    就像编译器在错误消息中所说的那样,p_div_ 确实期望Strings,而是HtmlT Data.Functor.Identity.Identity () 类型(某种字符串)。然而,这种类型是 IsString 类型类的成员,因此它可以被视为“类似字符串”的类型,并且有一个实现 [source code]

    instance (Monad m,a ~ ()) => IsString (HtmlT m a) where
      fromString = toHtml
    

    发生这种情况的原因是因为您可以添加 HTML 字符,在这种情况下,(p_ "&lt;foo&gt;") 看起来像:&lt;p&gt;&lt;foo&gt;&lt;/p&gt;。但这是非常不安全的。首先通过toHtml处理,结果将是&lt;p&gt;&amp;lt;foo&amp;gt;&lt;/p&gt;

    【讨论】:

      猜你喜欢
      • 2016-07-11
      • 1970-01-01
      • 1970-01-01
      • 2014-07-18
      • 2014-04-18
      • 2017-08-16
      • 2016-10-28
      • 1970-01-01
      • 2013-10-06
      相关资源
      最近更新 更多