-
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.
- Loading branch information
1 parent
a941a78
commit eed8348
Showing
3 changed files
with
25 additions
and
7 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,13 +1,26 @@ | ||
import ballerina/io; | ||
|
||
function foo() returns boolean { | ||
function col3() returns boolean { | ||
return false; | ||
} | ||
|
||
type MyCSVRawTemplate object { | ||
*object:RawTemplate; | ||
public (string[] & readonly) strings; | ||
public [int, int, boolean] insertions; | ||
}; | ||
|
||
public function main() { | ||
int x = 5; | ||
error y = error("foo"); | ||
object:RawTemplate rawTemplate = `x is ${x}. y is ${y}. The result of calling foo is ${foo()}`; | ||
int col1 = 5; | ||
int col2 = 10; | ||
|
||
// No static type validation for interpolation | ||
object:RawTemplate rawTemplate = `${col1}, ${col2}, ${col3()}`; | ||
io:println(rawTemplate.strings); | ||
io:println(rawTemplate.insertions); | ||
|
||
// validate interpolations at compile time | ||
MyCSVRawTemplate myCSVRawTemplate = `${col1}, ${col2}, ${col3()}`; | ||
io:println(myCSVRawTemplate.strings); | ||
io:println(myCSVRawTemplate.insertions); | ||
} |
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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
# Raw templates | ||
|
||
Raw template expressions are backtick templates without a tag (such as `string` or `xml`). This is a sequence of characters interleaved with interpolations within a pair of backticks (in the form`${expression}`). The result of evaluating such a raw template is a `RawTemplate` object that has two fields `(readonly & string[]) strings` and `(any|error)[] insertions`. The `strings` array will have string literals in the backtick string broken at interpolations and the `insertions` array will have the resultant values of evaluating each interpolation. | ||
Raw template expressions are backtick templates without a tag (such as `string` or `xml`). This is a sequence of characters interleaved with interpolations within a pair of backticks (in the form`${expression}`). The result of evaluating such a raw template is a `RawTemplate` object that has two fields `(readonly & string[]) strings` and `(any|error)[] insertions`. The `strings` array will have string literals in the backtick string broken at interpolations and the `insertions` array will have the resultant values of evaluating each interpolation. | ||
|
||
If you want to more precisely control the type of values used for interpolation you can define an object type that that includes `RawTemplate` type and give a more narrower type for `insertions` fields. Then compiler will statically verify corresponding values used for interpolation belong to the desired type. | ||
|
||
::: code raw_templates.bal ::: | ||
|
||
::: out raw_templates.out ::: | ||
|
||
## Related links | ||
- [Backtick templates](https://ballerina.io/learn/by-example/backtick-templates/) | ||
- [Object type inclusion](https://ballerina.io/learn/by-example/object-type-inclusion/) |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
$ bal run | ||
["x is ",". y is ",". result of calling foo is ",""] | ||
[5,error("foo"),false] | ||
["",", ",", ",""] | ||
[5,10,false] | ||
["",", ",", ",""] | ||
[5,10,false] |