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 conststring
, which inherits from string. However, its
static type is String
, so it cannot be assigned to a field of
type conststring
:
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 |
\" | " | 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 asString
to create an instance of String
from
any Fuzion instance. Any feature may redefine asString
to create a
string representation appropriate for the specific feature.
The operator prefix $
is defined as a synonym
for asString
, so a call $v
is shorthand
for v.asString
:
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
There is currently no support for multi line strings. Concatenation of several single line strings can help as a workaround. Note that the overall string has to be wrapped in parentheses since the expression would otherwise be limited to one line.