gpt4 book ai didi

haskell - Yesod 中模板 haskell 的评估

转载 作者:行者123 更新时间:2023-12-02 18:04:02 26 4
gpt4 key购买 nike

在浏览 Yesod Book 的示例时,我遇到了以下代码片段的问题:

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns #-}
import Data.Text (Text)
import qualified Data.Text as T
import Yesod

data App = App
instance Yesod App

mkYesod "App" [parseRoutes|
/person/#Text PersonR GET
/year/#Integer/month/#Text/day/#Int DateR
/wiki/*Texts WikiR GET
|]

getPersonR :: Text -> Handler Html
getPersonR name = defaultLayout [whamlet|<h1>Hello #{name}!|]

handleDateR :: Integer -> Text -> Int -> Handler Text -- text/plain
handleDateR year month day =
return $
T.concat [month, " ", T.pack $ show day, ", ", T.pack $ show year]

getWikiR :: [Text] -> Handler Text
getWikiR = return . T.unwords

main :: IO ()
main = warp 3000 App

(位于第 124 页(共 598 页);route arguments )

第 11 行的实例声明引发以下错误:

YesodRouteParams.hs:11:10: error:
• No instance for (RenderRoute App)
arising from the superclasses of an instance declaration
• In the instance declaration for ‘Yesod App’
|
11 | instance Yesod App
|

可以通过将该行移动到定义路由的 mkYesod block 下方来修复此问题。

我试图理解这是为什么。这是否意味着编译时的 Template Haskell 评估与编写的代码评估同时发生?

我问这个问题是因为例如在 Crystal 中,宏先于其他内容展开。因此,文件(或应用程序)中事物的顺序并不重要。但从表面上看,他们在 Haskell 中做到了。或者还有其他解释吗?

最佳答案

这是因为 GHC 9.0.1 中进行了更改。来自 the release notes :

Breaking change: Template Haskell splices now act as separation points between constraint solving passes. It is no longer possible to use an instance of a class before a splice and define that instance after a splice. For example, this code now reports a missing instance for C Bool:

class C a where foo :: a
bar :: Bool
bar = foo
$(return [])
instance C Bool where foo = True

如果您要降级到 GHC 8.10.7,您会发现您的代码将按照您编写的方式运行。

我打开了https://github.com/yesodweb/yesodweb.com-content/pull/269修复书中的示例。

关于haskell - Yesod 中模板 haskell 的评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73719275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com