String constants
Strings are enclosed by double quotes "
.
Simple Strings
A plain string declared with double quotes such as "some string"
can be assigned to fields of type String
:
Internally, a plain string is an instance of the standard library
feature Const_String
, which inherits from String
.
Nevertheless, the static type of a string constantant is String
.
You can see the difference when you compare the static type detemined
using type_of
with the dynamic type returned
by dynamic_type
:
Consequently, a string constnat can be assigned to a field of
type String
, but it cannot be assigned to a field of
type Const_String
:
Escaped Characters
A plain string must not contain any of the
characters $
, {
, }
, "
or \
. To include one of these characters, it must be escaped by
putting a backslash \
in front of it.
Also, a plain string must not contain any ASCII control characters in the
range of 0x00
..0x1f
nor the DEL
character
(0x7f
). In particular, a new line within a plain string is an
error.
To include any of these forbidden characters or certain ASCII control
characters, an escape sequence using a backslash \
can be used:
Escape sequence | character | ASCII code |
---|---|---|
\b | BS | 0x08 |
\t | HT | 0x09 |
\n | LF | 0x0a |
\f | FF | 0x0c |
\r | CR | 0x0d |
\s | SP | 0x20 |
\" | " | 0x22 |
\$ | $ | 0x24 |
\' | ' | 0x27 |
\\ | \ | 0x5c |
\{ | { | 0x7b |
\} | } | 0x7d |
Here is a small example using escaped control and special characters:
Converting instances to strings
All Fuzion features are heirs of Object
, which provides a
feature as_string
to create an instance of String
from
any Fuzion instance. Any feature may redefine as_string
to create a
string representation appropriate for the specific feature.
The operator prefix $
is defined as a synonym
for as_string
, so a call $v
is shorthand
for v.as_string
:
Embedded Expressions
Strings can be concatenated with the string representation of any instance
using infix +
:
Single identifiers or numeric literals can be embedded into a string literal
using a $
immediately before the identifier as follows.
This embedding using $
does not work for expressions that consist
of more than an identifier. For more complex expressions, you can use
curly braces {
and }
:
This works even for code that spans several lines:
Arbitrary nesting of strings and expressions is also possible:
Multi-line Strings
Multiline strings start and end with a fat quotation """
.
They mostly work just like 'normal' strings but have the following peculiarities.
- A multiline string is required to start in the first line following the fat quotation.
- The first line of the multiline string is the reference for the indentation to be used throughout the rest of the string. Thus all lines of the mulitline strings have to indented at least as much as the first line.
-
A multiline string may use interpolation with
$
and{}
just like 'normal' strings. - Arbitrary nesting of multiline strings is allowed.
- Trailing whitespace in multiline strings is disallowed.
- For adding whitespace at the beginning of the first line or at the end of lines use escape codes described above.
-
Line terminators in multiline strings are normalized to
LF
unlessCR
is explicitly included by using the escape codeCR
.