From 7c8380ad8213e9d1d2411b9552fdef75e67db040 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Thu, 19 Sep 2024 11:04:51 +0530 Subject: [PATCH] Add a separate section for expression equality bbe --- .../expression_equality.bal | 19 ++++++++--------- .../expression_equality.md | 4 ++-- examples/index.json | 21 ++++++++++++------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/examples/expression-equality/expression_equality.bal b/examples/expression-equality/expression_equality.bal index 9fb8561e57..59b8d6d82c 100644 --- a/examples/expression-equality/expression_equality.bal +++ b/examples/expression-equality/expression_equality.bal @@ -4,38 +4,38 @@ public function main() { map student = {"name": "John", "age": "25"}; map student2 = {"name": "John", "age": "25"}; - // The output will be `true` because the values are considered equal based on the + // The `==` check evaluates to `true` since the values are considered equal based on the // equality of members. io:println(student == student2); - // The output will be `false` because the values are considered equal based on the + // The `!=` check evaluates to `false` since the values are considered equal based on the // equality of members. io:println(student != student2); - // The output will be `false` because the references are different. + // The `===` check evaluates to `false` since references are different. io:println(student === student2); // Assign the value assigned to the `student` variable to the `student3` variable. map student3 = student; - // The output will be `true` because the references are the same. + // The `===` check evaluates to `true` since references are same. io:println(student3 === student); - // The output will be `false` because the references are the same. + // The `!==` check evaluates to `false` since references are same. io:println(student3 !== student); + // Since values of simple types do not have storage identity + // `===` and `==` return the same result, except for floating point values. int a = 1; anydata b = 1; // The output will be `true` because the values are equal. io:println(a == b); - // Since values of simple types do not have a storage identity, - // `===` will evaluate to `true` because the values are the same. io:println(a === b); decimal c = 1.0; decimal d = 1.00; - // `===` and `==`` are the same for simple values except for floating point types. + // The output will be `true` because the values are equal. io:println(c == d); // The output will be `false` because `c` and `d` are distinct values with different precision. @@ -44,8 +44,7 @@ public function main() { string s1 = "Hello"; string s2 = "Hello"; - // The string type is a sequence type, not a simple type, - // but `==` and `===` still behave the same for string comparisons + // The string type is a sequence type, but `===` and `==`` return the same result for string comparisons. io:println(s1 == s2); io:println(s1 === s2); } diff --git a/examples/expression-equality/expression_equality.md b/examples/expression-equality/expression_equality.md index f0a4e203c8..9b98e682e9 100644 --- a/examples/expression-equality/expression_equality.md +++ b/examples/expression-equality/expression_equality.md @@ -1,8 +1,8 @@ # Expression equality -In Ballerina, expression equality is determined using two operators: `==` for value equality and `===` for reference equality. The `==` operator checks for deep equality between two values by comparing the actual data. In contrast, the `===` operator checks whether two values share the same storage identity, meaning it checks if the values reference the same memory location. +Expression equality is determined using two operators: `==` for value equality and `===` for reference equality. The `==` operator checks for deep equality between two values by comparing the actual data. In contrast, the `===` operator checks whether two values share the same storage identity, meaning it checks if the values reference the same memory location. -Values with storage identity, such as structured types like maps and arrays, are stored as references, so `===` can determine if two references point to the same underlying data. For simple types such as integers and booleans, which do not have storage identity, `===` behaves the same as `==`. +Values with storage identity, such as structured types like maps and arrays, are stored as references, so `===` can determine if two references point to the same underlying data. For simple types such as integers and booleans, which do not have storage identity, `===` behaves the same as `==` except for floating point values. ::: code expression_equality.bal ::: diff --git a/examples/index.json b/examples/index.json index 9539640801..70d11f54ec 100644 --- a/examples/index.json +++ b/examples/index.json @@ -422,6 +422,20 @@ } ] }, + { + "title": "Equality", + "column": 0, + "category": "Language concepts", + "samples": [ + { + "name": "Expression equality", + "url": "expression-equality", + "verifyBuild": true, + "verifyOutput": true, + "isLearnByExample": true + } + ] + }, { "title": "Lists", "column": 1, @@ -482,13 +496,6 @@ "verifyBuild": true, "verifyOutput": true, "isLearnByExample": true - }, - { - "name": "Expression equality", - "url": "expression-equality", - "verifyBuild": true, - "verifyOutput": true, - "isLearnByExample": true } ] },