Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
Co-Authored-By: Maryam Ziyad <[email protected]>
  • Loading branch information
heshanpadmasiri and MaryamZi committed Nov 15, 2024
1 parent 0c7b30a commit 714c721
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
19 changes: 13 additions & 6 deletions examples/string-templates/string_templates.bal
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import ballerina/io;

public function main() {
string word = "world";
string s0 = string `Hello, ${word}!`;
io:println(s0);

// Use interpolations to use escape characters.
// Note how the first `\n` is treated as is.
string s1 = string `line one \n rest of line 1 ${"\n"} second line`;
io:println(s1);

// Use interpolations to add ` and $ characters.
// Use interpolations to add the ` and $ characters.
string s2 = string `Backtick: ${"`"} Dollar: ${"$"}`;
io:println(s2);

// Template expressions can be nested.
string s3 = string `outer ${string `inner ${5} inner rest`} rest`;
// A string template expression can be written on multiple lines by breaking at interpolations.
string s3 = string `T_1 is ${
1}, T_2 is ${1 +
2} and T_3 is ${1 + 2 + 3
}`;
io:println(s3);

// You can use an empty string interpolation to break a template expression
// across multiple lines.
// If there are no interpolations to break at a required point, you can introduce an interpolation with an empty
// string.
string s4 = string `prefix ${
""}middle ${""
}suffix`;
Expand Down
2 changes: 1 addition & 1 deletion examples/string-templates/string_templates.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# String templates

A string template is a template expression that can be used to create a string literal. It consists of a `string` tag and a sequence of characters interleaved with interpolations (in the form `${expression}`). Each interpolation must be a subtype of `boolean|int|float|decimal|string`. Every character not a part of the interpolation is interpreted as is to form a sequence of string literals broken at interpolations. This means if you want to add any escape characters you must add them as interpolations (for example `${"\n"}`). Every interpolation is converted to a string using the `toString` lang lib function and concatenated with the other string literals to form a single string literal.
A string template is a template expression that can be used to create a string literal. It consists of the `string` tag and a sequence of characters interleaved with interpolations (in the form `${expression}`). Each interpolation must be a subtype of `boolean|int|float|decimal|string`. Every character not a part of the interpolation is interpreted as is to form a sequence of string literals broken at interpolations. This means if you want to add any escape characters you must add them as interpolations (for example `${"\n"}`). Every interpolation is converted to a string using the `toString` lang lib function and concatenated with the other string literals to form a single string literal.
5 changes: 3 additions & 2 deletions examples/string-templates/string_templates.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
$ bal run string_templates.bal
line one \n rest of line 1
Hello, world!
line one \n rest of line 1
second line
Backtick: ` Dollar: $
outer inner 5 inner rest rest
T_1 is 1, T_2 is 3 and T_3 is 6
prefix middle suffix

0 comments on commit 714c721

Please sign in to comment.