THIS POST IS ARCHIVED

Raw Strings and String Interpolation

Our declarative modeling language Rel has been expanded to include new string functionalities to better handle and manipulate string data.

Two new string literal forms are now supported: raw string literals and interpolation expressions inside string literals.

Raw Strings

Raw string literals are string literals that keep their contents verbatim.

This allows users to provide complex strings (for example, for regular expressions) without any extra steps. No processing is applied and there is no need to escape special characters.

Complex strings are denoted by the raw identifier, immediately followed by some amount of odd quotes. The same amount of quotes must be used to terminate the literal. Anything in between is taken as is.

Special characters can appear without escaping, and escaping a character simply means two characters (\ and the escaped character). Any number of quotes that are less than the delimiting quotes can appear inside the literal.

Some examples are below.

Examples

The empty string. Note that this is the only form allowed for empty strings. Using more quotes results in a parser error:

raw""

Using quotes without escaping:

raw"""say, "hello" now"""

No need to use triple quotes for multiline strings:

raw"this is a multi 
   line 
text
"

Leading spaces are not discarded (as would be the case in “normal” string literals):

raw"
  Hello
    And bye
"

Using multiple quotes inside the string:

raw"""""many """"quotes here"""""

String Interpolation

Rel also now supports the arbitrary interpolation of expressions inside string literals.

The operator used for interpolation is %. As such, if the actual percent character is needed instead, it has to be escaped (or alternatively, be used in a raw string literal).

The result of the interpolation is a string, so arbitrary “nesting” is allowed too, as shown in the examples below.

Examples

Alternative to using raw"100% success rate":

"100\% success rate"

Interpolating values from an expression:

def names = "Alice"; "Bob"
def output = "hello %(names)"

If the interpolated expression is a single variable, parentheses are optional:

def output = "hello %names"

The outer string literal is interpolating an expression that itself is using string interpolation:

def from_id = ("user_1", "Alice"); ("user_2", "Bob")
def ids = 1;2;3
def output = "hello %(from_id["user_%ids"]) and bye"

We’re excited to learn about how you are using Rel --- please let us know about any future features you’d like to see.