【问题标题】:How to reconcile types between `hakyll` and `hakyll-images`如何协调`hakyll`和`hakyll-images`之间的类型
【发布时间】:2019-01-20 06:42:20
【问题描述】:

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

hakyll-images Readme 中的失败示例如下。

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
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

错误是因为loadImage 定义的类型Image 是compile 要求的类型类Writable 的实例。 hakyll和hakyll-images使用的函数类型,抄自hackage文档,如下所示。

    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-images 中定义为type Image = Image_ ByteString。 我不确定Image_ 是什么; Hakyll.Images module 的文档中没有链接它的定义。

无论如何,hakyll-images 的自述文件中的示例似乎无法编译,因为Image 不是Writable 的实例。我想知道hakyll-images 包是否在某些时候与hakyll 不同步导致示例不再编译。

这个评估看起来正确吗? 您对我应该如何解决问题有什么建议?

我正在考虑:

  • 通过为Image 添加一个Writable 实例来更新hakyll-images。
  • 使用其他一些函数集或函数组合来执行保持纵横比的图像缩放。
  • 放弃 hakyll-images 并寻找其他方法来缩放图像。

【问题讨论】:

    标签: haskell hakyll


    【解决方案1】:

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

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

    如果你想自己实现实例,你可以看看它是如何完成的here。

    【讨论】:

    • 啊,我看到我使用的是旧版本,它已在最新版本中修复。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多