Search code examples
rakustring-interpolation

Can sigil-less variables or constants be interpolated?


Although I have found no documentation for it, apparently you can interpolate sigilless variables and constants surrounding them by {}:

constant ⲧ = " " xx 4;
say "{ⲧ}Tabbed"; # OUTPUT: «       Tabbed␤»

Apparently, you need to use the :c adverb to do so.

say q:c"π is {π}"; # OUTPUT: «π is 3.141592653589793␤»

But that is only within if q (and derived) quotes are used. Is this simply an undocumented feature, or is something I'm missing here?


Solution

  • These are all exactly identical

              "a $b {$c}"
    qq        "a $b {$c}"
    Q :qq     "a $b {$c}"
    Q :double "a $b {$c}"
    
    Q :b :s :a :h :c :f "a $b {$c}"
    Q :backslash :scalar :array :hash :closure :function "a $b {$c}"
    

    In order for the string literal parser to see {} as creating a closure it needs the closure feature to be enabled.

    One of the things that the :qq / :double enables is :c / :closure.

    You can also disable it with :!closure.

    say qq :!closure "{ 1 + 2 }";
    # { 1 + 2 }
    

    That is it starts with :qq / :double semantics and turns off :closure semantics.


    Here is where the qq feature is defined in Rakudo

    role qq does b1 does c1 does s1 does a1 does h1 does f1 {
        token starter { \" }
        token stopper { \" }
        method tweak_q($v) { self.panic("Too late for :q") }
        method tweak_qq($v) { self.panic("Too late for :qq") }
    }
    

    b1 enables backslash
    c1 enables closure
    s1 enables scalar
    a1 enables array
    h1 enables hash
    f1 enables function


    It is documented, and you provided a link to the documentation.
    Perhaps it could be made more clear that:

    • "" is short for qq ""
    • qq "" is short for Q :qq "" / Q :double ""
    • :qq / :double is short for all of :backslash :closure :scalar :array :hash :function.

    Also it may be worth adding examples for all of those features.