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.push('!');
    // 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

{expr} embeds a value or an arbitrary expression in a string literal. Interpolated values must implement Printable. Write \{ for a literal brace.

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