Skip to content

Commit

Permalink
fix issues in pattern matching docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Jan 20, 2024
1 parent 7e1b9a7 commit db0f86c
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions _docs/basics/12-pattern-matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ permalink: /docs/pattern-matching/
---

The `match` expression can be used to exhaustively match a variable with its different possible values.
If you are are not familiar with the concept of `pattern matching` you can think of it as some kind of extended `switch ... case` expression.
If you are not familiar with the concept of `pattern matching` you can think of it as some kind of extended `switch ... case` expression.

We use it to `match` a variable or an expression with all of it's possible `pattern`s. For example this pattern matching expression can be used to match a single enum type of seasons.
We use it to `match` a variable or an expression with all of its possible `pattern`s. For example, this pattern-matching expression can be used to match a single enum type of seasons.

```fuse
const name = match season when
Expand All @@ -17,7 +17,7 @@ const name = match season when
end
```

Since a `match` expression must be exhaustive, We need to either check every possible values or if we cann't or don't need to we should include a `else` block, You can think of it as `default` in good old switch cases.
Since a `match` expression must be exhaustive, We need to either check every possible value or if we can't or don't need to we should include an `else` block, You can think of it as `default` in good old switch cases.

```fuse
match season when
Expand All @@ -26,7 +26,7 @@ match season when
end
```

If we cant to match a block to multiple patterns we can use `and` and `or` operator.
If we can't match a block to multiple patterns we can use the `and` and `or` operator.

```fuse
match season when
Expand All @@ -35,8 +35,8 @@ match season when
end
```

A type system with support for pattern matching should enforce the exhaustion of the subject of match. It means if we start using a product variable such as a `Tuple` or `struct` we get to check product value of all possible pattern for every value in our compund type.
For example while a `boolean` have 2 possible values `true` or `false`, and our Season enum contains 4 possible values; If we create a tuple of `(Season, boolean)` we have to check for `2 x 4 = 8` possible values. That's what we call a `product type`.
A type system with support for pattern matching should enforce the exhaustion of the subject of the match. It means if we start using a product variable such as a `Tuple` or `struct` we get to check the product value of all possible patterns for every value in our compound type.
For example while a `boolean` has 2 possible values `true` or `false`, and our Season enum contains 4 possible values; If we create a tuple of `(Season, boolean)` we have to check for `2 x 4 = 8` possible values. That's what we call a `product type`.

```fuse
match tuple when
Expand All @@ -54,8 +54,8 @@ match tuple when
end
```

A pattern on product types can check only some of values and capture others to use in the branch.
For example if we don't care about our boolean value and only want to match our Season we can do this.
A pattern on product types can check only some of the values and capture others to use in the branch.
For example, if we don't care about our boolean value and only want to match our Season we can do this.

```fuse
match tuple when
Expand All @@ -66,9 +66,9 @@ match tuple when
end
```

A `pattern` can also contain an optional if condition to further expand the pattern.
A `pattern` can also contain an optional condition to further expand the pattern.

For a tuple of `(Season, number)` containing the current season and day in that season, We can write the following match expression to only print the word `Yay!` for second month of `Autumn` and `Spring`.
For a tuple of `(Season, number)` containing the current season and day in that season, We can write the following match expression to only print the word `Yay!` for the second month of `Autumn` and `Spring`.

```fuse
match tuple when
Expand Down

0 comments on commit db0f86c

Please sign in to comment.