7

Sorry if this is documented somewhere, but I haven't been able to find it. When using brace delimiters with qq, code is not interpolated:

qq.raku

#!/usr/bin/env raku

say qq{"Two plus two": { 2 + 2 }};
say qq["Two plus two": { 2 + 2 }];
$ ./qq.raku 
"Two plus two": { 2 + 2 }
"Two plus two": 4

Obviously, this isn't a big deal since I can use a different set of delimiters, but I ran across it and thought I'd ask.

Update

As @raiph pointed out, I forgot to put the actual question: Is this the way it's supposed to work?

JustThisGuy
  • 1,109
  • 5
  • 10

2 Answers2

8

The quote language "nibbler" (the bit of the grammar that eats its way through a quoted string) looks like this:

    [
        <!stopper>
        [
        || <starter> <nibbler> <stopper>
        || <escape>
        || .
        ]
    ]*

That is, until we see a stopper, eat whichever comes first of:

  • A starter (the opening { in your case), followed by some internal stuff, followed by a stopper (the }); this allows for nesting of the construct inside of the string
  • An escape (and closure interpolation is considered a kind of escape)
  • Any other character

This ordering in the grammar means that a nesting of the chosen quote starter/stopper will always win over an escape. This issue was discussed during the language design; we could, after all, have reordered the alternation in the grammar to have escapes win. On balance, however, it was felt that the choice of starter/stopper was the more local decision than the general properties of the quoting language, and so should take precedence. (This is also consistent with how quote languages are constructed: we take the base quoted string grammar and mix starter/stopper methods into it.)

Jonathan Worthington
  • 29,104
  • 2
  • 97
  • 136
  • "a nesting of the chosen quote starter/stopper will always win over an escape." Yes, though, per my answer, if the nesting is inside an intermediary escape that re-enters the `Q` slang using a different delimiter, you can still get to use the `{...}` code escape even inside a `qq{...}`. – raiph Aug 23 '21 at 15:59
  • @Jonathan Thanks for the answer with the reasoning behind it! – JustThisGuy Aug 24 '21 at 00:06
  • 1
    @raiph Indeed; in that case the `` part eats the whole nested language part. – Jonathan Worthington Aug 24 '21 at 08:58
4

Obviously, this isn't a big deal since I can use a different set of delimiters, but I ran across it and thought I'd ask.

You didn't ask anything. :)

Let's say you've got some text. And you want to use double quote processing to get interpolation, except you don't want braced text to be interpolated as code. You could write, say, qq:!c '...'. But don't you think it's a lot easier to remember, write, and read qq{ ... }?

Nice little touch, right?

Which is why it's the way it is -- it's a very nice touch.

And, perhaps, why it's not documented -- it's little, and, once you encounter it, obvious what you need to do.

That said, the Q lang escapes include ones to recursively re-enter the Q lang:

say qq{"Two plus two": \qq[{ 2 + 2 }] }; # "Two plus two": 4 

Does that answer your question? :)

raiph
  • 31,607
  • 3
  • 62
  • 111