Skip to content

Commit

Permalink
Merge pull request #25 from MamboBryan/main
Browse files Browse the repository at this point in the history
Fixes Home
  • Loading branch information
MamboBryan authored Jun 11, 2023
2 parents 468d60e + cf364d6 commit dd323d2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
71 changes: 71 additions & 0 deletions bits/equality.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
title: Equality (== or ===)
sidebar_position: 7
---

### _Why do we have two equality checks?_
Because sometimes you want to check more than if the two object have the same value.

> == checks if two objects have the same value
> === checks if two objects have the same reference
| Type | Check | Name |
|:-------|:-------|:----------|
| <mark> == </mark> | same **value** | `structural` |
| <mark> === </mark> | same **reference** | `referential` |

### What do you mean?
Let's use an example
```kotlin
data class Author(val name: String = "Yoda")

val first = Author()
val second = Author()

println(first == second) // structural equality check

> true
```

> With <mark> structural</mark> equality it checks if the values in the two classes are the same, in this case it checks if second is of type `Author` and if `second.name` is the same with `first.name`. That's what we mean when we say it checks they have the same `value`
### _How does it work_?
> When we call `==` for any two objects it is translated to `first?.equals(second) ?: (second === null)` under the hood
- it checks if `first` is null
- if `first` **IS null**, it checks if `second` also references null
- if `first` **IS NOT null** it checks if all the values in `first` are the same with all the values in `second`

### _What about reference checks?_
Okay, let's go back to our example
```kotlin
data class Author(val name: String = "Yoda")

val first = Author()
val second = Author()

println(first === second) // referential equality check

> false
```

> A <mark>referential</mark> check validates if two variables point to the same object in memory. In the case above `first` and `second` point to different objects so it evaluates to `false`.
What if they point to the same object?
```kotlin
data class Author(val name: String = "Yoda")

val first = Author()
val second = first

println(first === second) // referential equality check

> true
```
> In this case, `first` and `second` point to the same object in memory, if the `name` property changes for the value both first and second will be updated. And this is what we mean when we say it checks the **_reference_** of two objects.
### _Anything else I should know_ ?
Structural equality checks work different with data classes. That requires a bit of it's own so I'll link it here when ready...

You can find out more in other bits, keep on browsing and add a reaction below or comment :wink: :wink:...
4 changes: 2 additions & 2 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ const config = {
navbar: {
title: "KotlinBits",
logo: {
alt: "My Site Logo",
alt: "KotlinBits",
src: "img/logo.svg",
},
items: [
{
to: "/bits/Intro",
to: "/bits/intro",
label: "Bits",
position: "left",
activeBaseRegex: `/docs/`,
Expand Down
7 changes: 5 additions & 2 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ function HomepageHeader() {
<p className="hero__subtitle">{siteConfig.tagline}</p>
<div className={styles.buttons}>
<Link
className="button button--secondary button--lg"
className="button button--secondary button--lg pr-2"
to="/bits/intro"
>
Bits
</Link>
<Link className="button button--secondary button--lg px-2" to="/blog">
<Link>
""
</Link>
<Link className="button button--secondary button--lg ml-2 pl-2" to="/blog">
Articles
</Link>
</div>
Expand Down

1 comment on commit dd323d2

@vercel
Copy link

@vercel vercel bot commented on dd323d2 Jun 11, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

kotlinbits – ./

kotlinbits-git-release-mambobryan.vercel.app
kotlinbits-mambobryan.vercel.app
kotlinbits.vercel.app

Please sign in to comment.