Haskell QuasiQuotes Text.RawString.QQ интерполяция
Как я могу интерполировать так:
{-# 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 }
печатает как литерал, а не как интерполяцию, могу ли я сделать что-то похожее, чтобы интерполировать в этом случае?
2 ответа
Квази квотер 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
Единственный способ достичь цели - объединить:
{-# 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"