-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add BBE for byte and expression equality #5634
Merged
gimantha
merged 12 commits into
ballerina-platform:master
from
SasinduDilshara:fix_9388
Sep 19, 2024
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
44b46d7
Add BBE for byte and expression equality
SasinduDilshara 3808050
Rename the byte_type files
SasinduDilshara faaf8de
Update comments in byte and expression equality
SasinduDilshara 13e522d
Update comments on the expression equality bbe
SasinduDilshara d0021ae
update the expression equality bbe
SasinduDilshara 1c5ae80
Update the description of the expression equality bbe
SasinduDilshara 80f2a32
Change the comments on the expression equality bbe
SasinduDilshara 070f77c
Add string and floating point examples
SasinduDilshara 7c8380a
Add a separate section for expression equality bbe
SasinduDilshara 000ece2
Update description in expression equality
SasinduDilshara 1ec4191
Add unsigned8 for metadata tags
SasinduDilshara ab8cb50
Add reference link for integer subtypes
SasinduDilshara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import ballerina/io; | ||
|
||
public function main() { | ||
// The `byte` type consists of integers ranging from `0` to `255`. | ||
byte b = 255; | ||
io:println(b); | ||
|
||
// Since the set of possible `byte` values is a subset of `int` values, | ||
// the `byte` type is a subtype of the `int` type. | ||
int i = b; | ||
io:println(i); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Byte type | ||
|
||
The byte type in Ballerina represents an 8-bit unsigned integer, with values ranging from 0 to 255. | ||
|
||
::: code byte_type.bal ::: | ||
|
||
::: out byte_type.out ::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
description: This BBE introduces the Ballerina byte type. | ||
keywords: ballerina, ballerina by example, bbe, bytes, byte, byte type | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
$ bal run byte_type.bal | ||
255 | ||
255 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import ballerina/io; | ||
|
||
public function main() { | ||
map<string> student = {"name": "John", "age": "25"}; | ||
map<anydata> student2 = {"name": "John", "age": "25"}; | ||
|
||
// The `==` check evaluates to `true` since the values are considered equal based on the | ||
// equality of members. | ||
io:println(student == student2); | ||
|
||
// The `!=` check evaluates to `false` since the values are considered equal based on the | ||
// equality of members. | ||
io:println(student != student2); | ||
|
||
// 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 `===` check evaluates to `true` since references are same. | ||
io:println(student3 === student); | ||
|
||
// 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 `==` and `===` checks evaluates to `true` since the values are equal. | ||
io:println(a == b); | ||
io:println(a === b); | ||
SasinduDilshara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
decimal c = 1.0; | ||
decimal d = 1.00; | ||
|
||
// The `==` check evaluates to `true` since the values are equal. | ||
io:println(c == d); | ||
// 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"; | ||
|
||
// 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Expression equality | ||
|
||
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, 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 ::: | ||
|
||
::: out expression_equality.out ::: | ||
|
||
## Related links | ||
- [Maps](/learn/by-example/maps) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
description: This BBE demonstrates expression equality checks in Ballerina. | ||
keywords: ballerina, ballerina by example, bbe, ==, !=, ===, !==, equality |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
$ bal run expression_equality.bal | ||
true | ||
false | ||
false | ||
true | ||
false | ||
true | ||
true | ||
true | ||
false | ||
true | ||
true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we add unsigned8 also? @SasinduDilshara @MaryamZi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case we may need to refer to https://ballerina.io/learn/by-example/built-in-integer-subtypes/ as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done