audreyt / interpolatedstring-perl6

QuasiQuoter for Perl6-style multi-line interpolated strings with q, qq and qc support.
http://hackage.haskell.org/package/interpolatedstring-perl6/
Creative Commons Zero v1.0 Universal
25 stars 8 forks source link

When do interpolated values get wrapped in double-quotes? #6

Closed saurabhnanda closed 6 years ago

saurabhnanda commented 7 years ago

We're using qc extensively in our project because it is hands-down the easiest way to interpolate stringy things. However, one issue we constantly keep running into is values getting wrapped in double-quotes. Sometime putting a Text type annotation makes the quotes go aways, sometimes not. eg.

[qc|Hi {user ^. name :: Text}|]

Is there some documentation describing why this happens, and how to be sure that the interpolated value isn't going to be wrapped in quotes?

saurabhnanda commented 7 years ago

Here's a real-life example...

successPage :: Storefront -> Html ()
successPage sf =
  doctypehtml_ $ do
    head_ $ do
      title_ ([qc|Payment successful | {sf ^. clientName}|]) -- THIS INTERPOLATION
      meta_ [name_ "viewport", content_ "width=device-width, initial-scale=1"]

      -- TODO: do we need this?
      link_ [rel_ "apple-toucht-icon", href_ "apple-touch-icon.png"]

...results in the following:

<!DOCTYPE HTML><html><head><title>Payment successful | &qout;REDACTED&quot;</title><meta content="width=device-width, initial-scale=1" name="viewport"><link href="apple-touch-icon.png" rel="apple-toucht-icon"></head></html>

Even though sf ^. clientName is actually Text, if I change the interpolation to the following, the wrapping quotes in the generated HTML go away:

title_ ([qc|Payment successful | {sf ^. clientName :: Text}|])
audreyt commented 7 years ago

Hmm, does putting :: String in place of :: Text still compile (and cause it to output double quotes) ?

iand675 commented 6 years ago

Just wanting to chime in that I'm encountering this issue as well.

audreyt commented 6 years ago

The double quote is usually put in by an extra show. This happens if the incoming type has no ShowQ instance. At the moment, this module instantiates Char, String, Data.ByteString.Char8.ByteString, Data.ByteString.Lazy.Char8.ByteString, Data.Text.Text and Data.Text.Lazy.Text.

If somehow your Text library differs from one of the above (or different from the version associated when this module is installed), then an extra pair of quotes will show up.

Defining your own ShowQ instance for that type will fix this issue, e.g.:

instance ShowQ Your.Text where
    showQ = Your.unpack