Quoting operators and their various uses.
Prog has a number of different quoting operators used to produce or represent different kinds of string data.
Basic strings can be enclosed in double quotes ("") or single quotes (''):
my_string = "Hello, world!"; other_string = 'other';
A double-quoted string can contain a single-quoted expression; the expression is evaluated in string context and the result embedded in place of the expression. Likewise, a single-quoted string can contain a double-quoted expression:
phrase = "Hello, 'other_string' world!"; phrase = 'Hello, "other_string" world!';
An expression embedded in a string can contain quoted strings as well, provided that the type of quote is alternated:
hello = "Hello, "; world = "world!"; result = "The length of 'hello''world' is 'length("'hello''world'")'.\n";
Any sequence of characters that does not contain a backquote (`) can be treated as an identifier by wrapping it in backquotes. This is often used for operator overloading:
`identifier with spaces` = 32; def `+`(a as Point, b as Point) { return Point(a.x + b.x, a.y + b.y); };
Note: to treat a single token as an identifier, it is possible to use \ rather than ``:
def \+(a as Point, b as Point) {};
Meta-quoting operators begin with a $ character followed by an identifier that determines their type, then a nonwhitespace character indicating the string delimiter. If the delimiter is a bracketing operator such as ( or <, the string is terminated by a corresponding ) or >. The string can contain the delimiter, but it must be escaped with \ if the operator would not be balanced. If the delimiter is a double or single quote, they cannot be nested as with ordinary strings. If the delimiter is any other character, the terminating delimiter is the same:
$e"UTF-8" $i/foo/ $x(Foo = (foo))
Note that if string literals interpolate non-const expressions, they cease to be const. Consider interpolation the same as concatenation in this regard.
It is possible to create custom meta-quoting operators. See Operator Overloading.
Where short and long names are both given for quoting operators, the strict string pragma disables the short names in favour of the long ones.
Returns an encoding type that, when applied to char, imbues the char with a specific character encoding.
Returns an identifier. This can be used to get around the restriction on backquoted identifiers containing backquotes. There is no $i(), because $id() is short enough.
Returns a language type that, when applied to string, imbues the string with a specific locale. There is no $l(), to avoid ambiguity between the digit one and the letter L.
Returns a string without performing interpolation.
Returns a string that is treated as a regular expression, i.e., a regex string, which can be used with the matching operator (=~) to test for a match or as the left operand of an assignment to perform a replacement. See Pattern Matching.
Returns a tuple:string by splitting a string along whitespace, ignoring leading and trailing whitespace:
$s(foo bar baz) == ("foo", "bar", "baz");
Returns a string, performing extended interpolation as with ordinary strings, but extending the concept of alternating quotes to nested bracketing delimiters ((), <>, etc.). $x cannot be used with any delimiters other than "", '', (), [], {}, and <>:
foo = $x[2 + 3 == [2 + 3]]; say = $x<The value of "foo" is "<foo>".>;