gpt4 book ai didi

haskell - 带有重载标签的 QuasiQuotes

转载 作者:行者123 更新时间:2023-12-03 19:03:55 24 4
gpt4 key购买 nike

我正在尝试将 OverloadedLabelshere 包中的 QuasiQuotes 一起使用。使用普通镜头可以,但 #foo 在编译期间因解析错误而失败。 field @"foo" 也是如此。是否有更深层次的原因,这将不起作用,或者它可能是 here 的插值解析器中的错误?

{-# language DataKinds #-}
{-# language DeriveGeneric #-}
{-# language DerivingStrategies #-}
{-# language OverloadedLabels #-}
{-# language OverloadedStrings #-}
{-# language TypeApplications #-}
{-# language QuasiQuotes #-}

import Control.Lens
import Data.Text (Text)
import qualified Data.Text.IO as T
import Data.Generics.Product
import Data.Generics.Labels
import Data.String.Here
import GHC.Generics (Generic)

data Test = Test
{ name :: Text
} deriving stock (Eq, Show, Generic)

_name :: Lens' Test Text
_name f (Test a) = fmap (\a' -> Test a') (f a)

t :: Test
t = Test "test"

test :: IO ()
test = do
-- ok
T.putStrLn $ t ^. field @"name"
T.putStrLn $ t ^. #name
putStrLn [i|${t ^. _name}|]

-- parse error
putStrLn [i|The name is ${t ^. field @"name"}|]
putStrLn [i|The name is ${t ^. #name}|]

#name 错误:

test.hs:36:12: error:
• Exception when trying to run compile-time code:
Failed to parse interpolated expression in string: The name is ${t ^. #name}
(line 1, column 25):
0
SrcLoc "" 1 6
Parse error in expression: t ^.

CallStack (from HasCallStack):
error, called at src/Data/String/Here/Interpolated.hs:64:33 in here-1.2.13-HU0AD0x0dD36rY9YuL1gwE:Data.String.Here.Interpolated
Code: Language.Haskell.TH.Quote.quoteExp
i "The name is ${t ^. #name}"
• In the quasi-quotation: [i|The name is ${t ^. #name}|]
|
36 | putStrLn [i|The name is ${t ^. #name}|]
|

最佳答案

看起来 haskell-src-meta 还不支持 OverloadedLabelshaskell-src-exts 解析器有 an OverloadedLabels case ,但是 haskell-src-metathe ToExp instance for Exp 中没有它的案例.我猜 haskell-src-meta 中的“不受支持”错误消息正在被 here 中的错误处理吞没。

关于haskell - 带有重载标签的 QuasiQuotes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64155741/

24 4 0