Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brace Delimiters with qq Don't Interpolate Code in Raku

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?

like image 731
JustThisGuy Avatar asked Aug 22 '21 23:08

JustThisGuy


2 Answers

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.)

like image 93
Jonathan Worthington Avatar answered Nov 17 '22 03:11

Jonathan Worthington


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? :)

like image 39
raiph Avatar answered Nov 17 '22 03:11

raiph