-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5819 from heshanpadmasiri/feat/backtick-template
Improve the explanation about backtick templates
- Loading branch information
Showing
10 changed files
with
49 additions
and
45 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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 the ` and $ characters. | ||
string s2 = string `Backtick: ${"`"} Dollar: ${"$"}`; | ||
io:println(s2); | ||
|
||
// 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); | ||
|
||
// 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`; | ||
io:println(s4); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# String templates | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
description: This BBE demonstrates string templates in Ballerina. | ||
keywords: ballerina, ballerina by example, bbe, string templates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
$ bal run string_templates.bal | ||
Hello, world! | ||
line one \n rest of line 1 | ||
second line | ||
Backtick: ` Dollar: $ | ||
T_1 is 1, T_2 is 3 and T_3 is 6 | ||
prefix middle suffix |