Skip to content

Commit

Permalink
Add a better example
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Oct 21, 2024
1 parent a941a78 commit 0f2d410
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
21 changes: 17 additions & 4 deletions examples/raw-templates/raw_templates.bal
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 are of correct type at compile time
MyCSVRawTemplate myCSVRawTemplate = `${col1}, ${col2}, ${col3()}`;
io:println(myCSVRawTemplate.strings);
io:println(myCSVRawTemplate.insertions);
}
5 changes: 4 additions & 1 deletion examples/raw-templates/raw_templates.md
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/)
6 changes: 4 additions & 2 deletions examples/raw-templates/raw_templates.out
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]

0 comments on commit 0f2d410

Please sign in to comment.