nikita-volkov / neat-interpolation

A quasiquoter for neat and simple multiline text interpolation
http://hackage.haskell.org/package/neat-interpolation
MIT License
55 stars 16 forks source link

Interpolation does not work for expressions #20

Closed fizruk closed 4 years ago

fizruk commented 6 years ago

Somewhat surprisingly, interpolation only works for variables, not expressions:

>>> import qualified Data.Text as Text
>>> n = 123 :: Int
>>> s = Text.pack (show n)
>>> [text| ${s} |]
"123\n"
>>> [text| ${Text.pack (show n)} |]
"${Text.pack (show n)}\n"
neongreen commented 6 years ago

This is because otherwise neat-interpolation would have to include a Haskell parser. There's no non-hacky way (AFAIK) to ask GHC to parse and execute some substring of a quasiquote at compile time.

There are other libraries though that use haskell-src-meta for that, but I'm actually glad that neat-interpolation doesn't – haskell-src-meta takes too much time and memory to compile.

It's also possible to implement a small parser for a subset of Haskell though and use that. Yesod went that way for its templating language, and Nikita's own record library also used to do that.

On Sun, Aug 12, 2018, 13:59 Nickolay Kudasov notifications@github.com wrote:

Somewhat surprisingly, interpolation only works for variables, not expressions:

import qualified Data.Text as Text n = 123 :: Int s = Text.pack (show n) [text| ${s} |] "123\n" [text| ${Text.pack (show n)} |] "${Text.pack (show n)}\n"

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/nikita-volkov/neat-interpolation/issues/20, or mute the thread https://github.com/notifications/unsubscribe-auth/ABc-aiUcEKwT2CzzzAgpgmJcyf6sqN4tks5uQBi5gaJpZM4V5gzf .

fizruk commented 6 years ago

Thank you for the explanation! Perhaps it should be added to the documentation :)

I have switched to interpolate for now, although it too does not fully meet my needs.