Pyro

An experimental programming language.

Version 0.3.2

String Formatting


Use the $fmt() function to interpolate values into a string:

$fmt(format_string, arg1, arg2, ...)

Returns the new string created by interpolating the argument values into the format string.

A format string is a string containing {} placeholders:

var foo = $fmt("{} and {}", 123, 456);
assert foo == "123 and 456";

var bar == $fmt("{} and {}", "foo", "bar");
assert bar == "foo and bar";

An empty placeholder {} is equivalent to calling $str(arg) on the value and interpolating the resulting string.

A placeholder can contain a format specifier — if the value has a $fmt(spec) method defined, this method will be called and the resulting string will be used.

(The format specifier itself is an arbitrary string — it's simply the content of the placeholder. How it's interpreted is up the value's $fmt(spec) method.)

For integers and floats the format specifier is handled directly by the C runtime so you can use printf–style formatting directives:

assert $fmt("{%.2f}", 1.23456) == "1.23";
assert $fmt("{%04i}", 99) == "0099";

Example

Here's a sample class with custom $str() and $fmt(spec) methods:

class Foo {
    def $str() {
        return "<foo>";
    }

    def $fmt(spec) {
        return "<" + spec + ">";
    }
}

Calling $str(arg) on an instance returns the output of its $str() method:

var foo = Foo();
assert $str(foo) == "<foo>";

An empty placeholder in a format string is equvialent to calling $str(arg) on the value:

assert $fmt("{}", foo) == "<foo>";

If the placeholder contains a format specifier, it gets passed to the value's $fmt(spec) method:

assert $fmt("{xyz}", foo) == "<xyz>";