Skip to content

Commit

Permalink
Update description in expression equality
Browse files Browse the repository at this point in the history
  • Loading branch information
SasinduDilshara committed Sep 19, 2024
1 parent ce24cb6 commit 34b0974
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions examples/expression-equality/expression_equality.bal
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@ public function main() {
int a = 1;
anydata b = 1;

// The output will be `true` because the values are equal.
// The `==` and `===` checks evaluates to `true` since the values are equal.
io:println(a == b);
io:println(a === b);

decimal c = 1.0;
decimal d = 1.00;

// The output will be `true` because the values are equal.
// The `==` check evaluates to `true` since the values are equal.
io:println(c == d);
// The output will be `false` because `c` and `d` are distinct values with different precision.
// The `===` check evaluates to `false` since `c` and `d` are distinct values with different precision.
io:println(c === d);

string s1 = "Hello";
string s2 = "Hello";

// The string type is a sequence type, but `===` and `==`` return the same result for string comparisons.
// String values also do not have storage identity, and `===` checks are
// the same as `==` checks for string values also.
io:println(s1 == s2);
io:println(s1 === s2);
}
2 changes: 1 addition & 1 deletion examples/expression-equality/expression_equality.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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 `==` except for floating point values.
Values with storage identity, such as structured types like maps and arrays, have an identity that comes from the location where the value is stored. For such values, the `===` check determines if two references point to the same location. 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

0 comments on commit 34b0974

Please sign in to comment.