-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve the explanation about backtick templates #5819
base: master
Are you sure you want to change the base?
Improve the explanation about backtick templates #5819
Conversation
Quality Gate passedIssues Measures |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need a separate BBE? Can't we just add https://ballerina.io/learn/by-example/regexp-type/ as a related link?
import ballerina/io; | ||
|
||
public function main() { | ||
// Note first \n is treated as is. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Note first \n is treated as is. | |
// 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); | ||
|
||
// You can use interpolations to add ` and $ characters. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to emphasize the characters using backticks. Need to check if/how this can be done.
string s1 = string `line one \n rest of line 1 ${"\n"} second line`; | ||
io:println(s1); | ||
|
||
// You can use interpolations to add ` and $ characters. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The form needs to be changed IMO.
// You can use interpolations to add ` and $ characters. | |
// Use interpolations to add ` and $ characters. |
string s2 = string `Backtick: ${"`"} Dollar: ${"$"}`; | ||
io:println(s2); | ||
|
||
// You can nest template expressions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// You can nest template expressions. | |
// Template expressions can be nested. |
io:println(s3); | ||
|
||
// You can use an empty string interpolation to break a template expression | ||
// across multiple lines |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or break at interpolations.
Let's add examples with both.
Also need a fullstop at the end.
@@ -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 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` method and concatenated with the other string literals to form a single string literal. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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` method 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 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. |
Purpose
$subject
Resolves ballerina-platform/ballerina-dev-website#9281
Goals
Approach
User stories
Release note
Documentation
Training
Certification
Marketing
Automation tests
Security checks
Samples
Related PRs
Migrations (if applicable)
Test environment
Learning