Skip to content

Commit

Permalink
Add a separate section for expression equality bbe
Browse files Browse the repository at this point in the history
  • Loading branch information
SasinduDilshara committed Sep 19, 2024
1 parent 59b928c commit ce24cb6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
19 changes: 9 additions & 10 deletions examples/expression-equality/expression_equality.bal
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@ public function main() {
map<string> student = {"name": "John", "age": "25"};
map<anydata> 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<string> 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.
Expand All @@ -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);
}
4 changes: 2 additions & 2 deletions examples/expression-equality/expression_equality.md
Original file line number Diff line number Diff line change
@@ -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 :::

Expand Down
21 changes: 14 additions & 7 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": 0,
Expand Down Expand Up @@ -482,13 +496,6 @@
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
},
{
"name": "Expression equality",
"url": "expression-equality",
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
}
]
},
Expand Down

0 comments on commit ce24cb6

Please sign in to comment.