首页 > 解决方案 > 如何协调`hakyll`和`hakyll-images`之间的类型

问题描述

我正在尝试使用hakyllhakyll-images实现hakyll-images自述文件中的一个示例,该示例执行我需要做的图像缩放。给定示例的类型不统一,我正在寻求有关如何进行的建议。

hakyll-images自述文件中的失败示例如下。

import Hakyll
import Hakyll.Images        ( loadImage
                            , scaleImageCompiler
                            )
main = hakyll $ do
    -- Scale images to fit within a 600x400 box
    -- Aspect ratio will be preserved
    match "images/*" $ do
        route idRoute
        compile $ loadImage
            >>= scaleImageCompiler 600 400

尝试编译会报错:

site.hs:12:9: error:
    • No instance for (Writable
                         hakyll-images-0.3.1:Hakyll.Images.Common.Image)
        arising from a use of ‘compile’
    • In a stmt of a 'do' block:
        compile $ loadImage >>= scaleImageCompiler 600 400
      In the second argument of ‘($)’, namely
        ‘do route idRoute
            compile $ loadImage >>= scaleImageCompiler 600 400’
      In a stmt of a 'do' block:
        match "images/*"
          $ do route idRoute
               compile $ loadImage >>= scaleImageCompiler 600 400
   |
12 |         compile $ loadImage >>= scaleImageCompiler 600 400
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

错误是因为Image由 定义的typeloadImage必须compile是 typeclass 的实例Writablehakyll从 hackage 文档中复制的和使用的函数类型hakyll-images如下所示。

    route :: Routes -> Rules ()
    idRoute :: Routes
    compile :: (Binary a, Typeable a, Writable a) => Compiler (Item a) -> Rules ()
    loadImage :: Compiler (Item Image)
    scaleImageCompiler :: Width -> Height -> Item Image -> Compiler (Item Image)

Image定义hakyll-imagestype Image = Image_ ByteString。我不确定是什么Image_;它的定义未在该Hakyll.Images模块的文档中链接。

无论如何,hakyll-images由于Image不是Writable. 我想知道是否可能hakyll-images包在某些时候变得不同步hakyll导致示例不再编译。

这个评估似乎正确吗?您对我如何解决解决方案有什么建议?

我正在考虑:

标签: haskellhakyll

解决方案


这种行为是一个进入 hakyll-images 0.3.1 版本的错误。随后在 hakyll-images 0.4 及更高版本中对其进行了修复。只需更新到最新版本即可解决此问题。

这是一个严重的疏忽,并且已经添加了测试,这样就不会再发生这种情况了。

如果您想自己实现这些实例,您可以在此处查看它是如何完成的。


推荐阅读