String Formatting
Formatting strings is an incredibly common programming task, whether it be for the sake of debugging or for building full-fledged CLI programs. Claro attempts to simplify this process by providing syntax sugar for directly templating arbitrary expressions directly into a string.
Note: At the moment, Claro only supports single-line strings, but multi-line strings are planned. Stay tuned for this in a future release.
To take advantage of this, any expression can be formatted into a string by wrapping it in {...}
.
Fig 1:
var s = "!EXAMPLE!";
print("prefix---{s}--suffix");
provider foo() -> string {
return "WOW!";
}
# Formatted expressions really are arbitrary expressions, you can even call
# arbitrary procedures from them.
print("prefix---{foo()}---suffix");
# Format strings can even be nested, though you should probably have a good
# reason for doing this.
print("prefix---{"inner+++{s}+++inner"}---suffix");
# You can also use multiple fmt arg expressions!
print("Name:\t{getRandomName()}\nAge:\t{random::nextNonNegativeBoundedInt(random::forSeed(1), 20)}");
provider getRandomName() -> string {
# ...
var names = ["Milo", "Millie", "Ruby"];
random::forSeed(2)
|> random::nextNonNegativeBoundedInt(^, 3)
|> var ind = ^;
return names[ind];
}
Output:
prefix---!EXAMPLE!--suffix
prefix---WOW!---suffix
prefix---inner+++!EXAMPLE!+++inner---suffix
Name: Millie
Age: 5
Escaping Curly-Braces in Strings
While Format Strings are very convenient, this does have the consequence of giving curly-braces a special significance
in string literals. So, to type a string literal that contains the {
char, you must escape it using \{
, for example:
Fig 2:
print("This string has \{literal \{curlies} in it}");
Output:
This string has {literal {curlies} in it}