Skip to content
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
merged 12 commits into from
Sep 19, 2024
Merged
3 changes: 1 addition & 2 deletions examples/binary-data/binary_data.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ Binary data is represented by arrays of byte values. It is a special syntax for

## Related links
- [Arrays](/learn/by-example/arrays)
- [Byte type](/learn/by-example/byte-type)
- [Integers](/learn/by-example/integers)

[comment]: # (Add byte type BBE link)
11 changes: 11 additions & 0 deletions examples/byte-type/byte_type.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ballerina/io;

public function main() {
// Byte values ranging from 0 to 255
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
byte b = 255;
io:println(b);

// Byte values can be assigned to int
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
int i = b;
io:println(i);
}
7 changes: 7 additions & 0 deletions examples/byte-type/byte_type.md
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 :::
2 changes: 2 additions & 0 deletions examples/byte-type/byte_type.metatags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description: This BBE introduces Ballerina byte type.
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
keywords: ballerina, ballerina by example, bbe, bytes, byte, byte type
Copy link
Contributor

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

3 changes: 3 additions & 0 deletions examples/byte-type/byte_type.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$ bal run byte_type.bal
255
255
33 changes: 33 additions & 0 deletions examples/expression-equality/expression_equality.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import ballerina/io;

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 the same.
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
io:println(student == student2);

// The output will be `false` because the values are the same.
io:println(student != student2);

// The output will be `false` because the references are different.
io:println(student === student2);

// This assigns the reference of `student` to `student3`.
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
map<string> student3 = student;

// The output will be `true` because the references are the same.
io:println(student3 === student);

// The output will be `false` because the references are the same.
io:println(student3 !== student);

int a = 1;
anydata b = 1;

// The output will be `true` because the values are the same.
io:println(a == b);
// Since simple type values do not have a storage identity,
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
// `===` will return `true` because the values are the same.
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
io:println(a === b);
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
}
11 changes: 11 additions & 0 deletions examples/expression-equality/expression_equality.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 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 they store. In contrast, the `===` operator checks whether two values share the same storage identity, meaning it compares whether the values reference the same memory location.
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
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 like, such as integers and booleans, which do not have storage identity, `===` behaves the same as `==`.
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved

::: code expression_equality.bal :::

::: out expression_equality.out :::

## Related links
- [Maps](/learn/by-example/maps)
2 changes: 2 additions & 0 deletions examples/expression-equality/expression_equality.metatags
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
8 changes: 8 additions & 0 deletions examples/expression-equality/expression_equality.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$ bal run expression_equality.bal
true
false
false
true
false
true
true
14 changes: 14 additions & 0 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
},
{
"name": "Byte type",
"url": "byte-type",
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
}
]
},
Expand Down Expand Up @@ -475,6 +482,13 @@
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
},
{
"name": "Expression equality",
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
"url": "expression-equality",
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
}
]
},
Expand Down
3 changes: 1 addition & 2 deletions examples/list-equality/list_equality.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ You can use `==` and `!=` on lists to check the deep equality of two lists: two
## Related links
- [Tuples](/learn/by-example/tuples)
- [Arrays](/learn/by-example/arrays)
- [Expression equality](/learn/by-example/expression-equality)
- [List sub typing](/learn/by-example/list-subtyping)

[comment]: # (Add equality expression link)
Loading