Skip to content

Commit

Permalink
improve README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
yceruto committed May 2, 2024
1 parent dde9fe8 commit 257bb0c
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![License](https://poser.pugx.org/yceruto/option-type/license)](https://packagist.org/packages/yceruto/option-type)
[![PHP Version Require](https://poser.pugx.org/yceruto/option-type/require/php)](https://packagist.org/packages/yceruto/option-type)

The `Option` class represents a value that might or might not be there. It's all about
The `Option` type represents a value that might or might not be there. It's all about
null safety in PHP!

> [!NOTE]
Expand All @@ -24,9 +24,6 @@ In PHP, denoting the absence of a value is done with `null`, e.g. when a `divide
function returns `null` if the divisor is `0`.

```php
/**
* @return int|null
*/
function divide(int $dividend, int $divisor): ?int
{
if (0 === $divisor) {
Expand All @@ -50,13 +47,13 @@ divide by zero. The function will return `null`, and the `success()` function wi
a `TypeError` because it expects an `int` value, not `null`.

The issue with this approach is that it's too easy to overlook checking if the value is
`null`, leading to runtime errors. This is where the `Option` class comes in handy: it
makes you deal with the possibility of a missing value, the `null` case.
`null`, leading to runtime errors, and this is where the `Option` type comes in handy: it
always forces you to deal with the `null` case.

## Handling the presence or absence of a value with `Option`

Options often work with pattern matching to check if there’s a value and act accordingly,
always making sure to handle the `None` case.
always making sure to handle the `null` case.

```php
use Std\Type\Option;
Expand Down

0 comments on commit 257bb0c

Please sign in to comment.