Haskell QuasiQuotes Text.RawString.QQ интерполяция - PullRequest
0 голосов
/ 04 июня 2018

Как я могу интерполировать следующим образом:

{-# 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 ]

0 голосов
/ 06 июня 2018

Квази-квотер 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
0 голосов
/ 05 июня 2018

Единственный способ достичь цели - объединить:

{-# 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"
...