From 8a67d59dd2d19c3eb05f483f76e3b92ae982629b Mon Sep 17 00:00:00 2001 From: Heshan Padamsiri Date: Wed, 13 Nov 2024 08:10:28 +0530 Subject: [PATCH] Address review comments Co-Authored-By: Maryam Ziyad --- .../string-templates/string_templates.bal | 19 +++++++++++++------ examples/string-templates/string_templates.md | 2 +- .../string-templates/string_templates.out | 5 +++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/examples/string-templates/string_templates.bal b/examples/string-templates/string_templates.bal index 35a748f2ef..6db6ead5c5 100644 --- a/examples/string-templates/string_templates.bal +++ b/examples/string-templates/string_templates.bal @@ -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 and empty + // string. string s4 = string `prefix ${ ""}middle ${"" }suffix`; diff --git a/examples/string-templates/string_templates.md b/examples/string-templates/string_templates.md index db2bcae0e1..de854c2bf5 100644 --- a/examples/string-templates/string_templates.md +++ b/examples/string-templates/string_templates.md @@ -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. diff --git a/examples/string-templates/string_templates.out b/examples/string-templates/string_templates.out index b56c2c75f9..422e868db7 100644 --- a/examples/string-templates/string_templates.out +++ b/examples/string-templates/string_templates.out @@ -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