我如何像这样进行插值:
{-# LANGUAGE QuasiQuotes #-}
import Text.RawString.QQ
myText :: Text -> Text
myText myVariable = [r|line one
line two
line tree
${ myVariable }
line five|]
myText' :: Text
myText' = myText "line four"${ myVariable }打印为文字,而不是插值,在这种情况下,我可以做一些类似于插值的事情吗?
发布于 2018-06-06 11:08:16
Quasi r不实现插值。它只适用于原始字符串。您需要另一个准引号。
完整代码:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
import Data.Text (Text)
import Text.RawString.QQ (r)
import NeatInterpolation (text)
rQuote :: Text -> Text
rQuote myVariable = [r|line one
line two
line tree
${ myVariable }
line five|]
neatQuote :: Text -> Text
neatQuote myVariable = [text|line one
line two
line tree
$myVariable
line five|]
rText, neatText :: Text
rText = rQuote "line four"
neatText = neatQuote "line four"在ghci中
*Main> import Data.Text.IO as TIO
*Main TIO> TIO.putStrLn rText
line one
line two
line tree
${ myVariable }
line five
*Main TIO> TIO.putStrLn neatText
line one
line two
line tree
line four
line five发布于 2018-06-05 19:16:51
我实现目标的唯一方法就是连接:
{-# LANGUAGE QuasiQuotes #-}
import Text.RawString.QQ
myText :: Text -> Text
myText myVariable = [r|line one
line two
line tree
|] <> myVariable <> [r|
line five|]
myText' :: Text
myText' = myText "line four"https://stackoverflow.com/questions/50688200
复制相似问题