Strings

cx has two string types:

void main() {
    var a = "test";
    // a has type 'string'

    var b = StringBuf(a);
    // b holds a copy of "test" that we can mutate

    b.append('!');
    // b now holds "test!", a is unchanged

    println(a); // prints "test"
    println(b); // prints "test!"
}

A StringBuf converts to a string view implicitly, so buffers pass directly to functions taking strings:

int stringSize(string s) {
    return s.size();
}

void main() {
    var buffer = StringBuf("hello");
    println(stringSize(buffer)); // prints 5
}

Interpolation

$name embeds a value and ${expr} an arbitrary expression in a string literal. Interpolated values must implement Printable. Write $$ for a literal dollar sign.

void main() {
    var name = "world";
    println("hello $name!"); // prints "hello world!"
    println("1 + 2 = ${1 + 2}"); // prints "1 + 2 = 3"
    println("$$5"); // prints "$5"
}