-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
37 additions
and
40 deletions.
There are no files selected for viewing
File renamed without changes.
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 |
---|---|---|
@@ -1,17 +1,18 @@ | ||
# Struct | ||
|
||
Structs are new types composed of other types. | ||
Struct is a new type composed of other types. | ||
|
||
In the example we define a struct `Point` with two fields, `x` and `y`, both of which are integers. | ||
In the example we define a struct `Point` with two fields, `x` and `y`, both of which are integers. | ||
|
||
We can create an instance of `Point` by using the `{ x: 3, y: 4}`. The structs name can be omitted since the compiler can infer it from the labels `x` and `y`. | ||
We can create an instance of `Point` by using the `{ x: 3, y: 4 }`. The struct name can be omitted since the compiler can infer it from the labels `x` and `y`. | ||
|
||
We can also add a `Point::` prefix to create an instance explicitly to disambiguate. | ||
|
||
Analogous to tuples, we can access the fields of a struct using the syntax `point.x`. | ||
|
||
The `derive(Show)` after the struct definition means that we can print the struct using the `println` function. | ||
The `derive(Show)` after the struct definition means that we can print the struct using the `println` function. | ||
|
||
The fields of a struct are immutable by default; they can't be changed after they are created. There is a syntax called _functional update_ that allows you to create a new struct with some fields updated. | ||
The fields of a struct are immutable by default; they can't be changed after they are created. There is a syntax called *functional update* that allows you to create a new struct with some fields updated. | ||
|
||
We will learn how to make the fields mutable in the next lesson. | ||
|
File renamed without changes.
File renamed without changes.
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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
# Result | ||
|
||
Similar to `Option[Char]`, `Result[Char, String]` represents a `Char` value that may or may not be present. If not, it can contain an error message of type `String`. | ||
Similar to `Option[Char]`, the enum `Result[Char, String]` represents a `Char` value that may or may not be present. Otherwise, it can contain an error message of type `String`. | ||
|
||
- `Err("error message")` means the value is missing, and the error message is provided. | ||
- `Ok('h')` is a wrapper that contains the value `'h'`. | ||
|
||
The processing of `Option` and `Result` in examples so far is verbose and prone to bugs. To handle `Option` and `Result` values safely and cleanly, you can use pattern matching. It's recommended to use *error handling* to process errors effectively. These two topics will be covered in a later chapter. | ||
The processing of `Option` and `Result` in examples so far is verbose and prone to bugs. To handle `Option` and `Result` values safely and cleanly, you can use *pattern matching*. It's recommended to use *error handling* to process errors effectively. These two topics will be covered in a later chapter. | ||
|
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
File renamed without changes.
File renamed without changes.
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
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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
# Tuple | ||
|
||
Tuple is a collection of values, which can have different types. It is immutable, | ||
Tuple is a collection of values that can have different types. It is immutable, | ||
which means that once it is created, it cannot be changed. It is created using | ||
parentheses. | ||
|
||
You can access the elements of tuple via the index: `tuple.0`, `tuple.1`, etc. | ||
|
||
Tuple can be destructured via syntax like `let (a,b) = tuple`, the `tuple` in | ||
Tuple can be destructed via syntax like `let (a,b) = tuple`, the `tuple` in | ||
right side is a tuple with two elements, and `a` and `b` are the variables to | ||
store the elements. This is a special use case of pattern matching. We will | ||
introduce pattern matching in the later chapter. | ||
store the elements. This is a special use case of *pattern matching*. We will | ||
introduce *pattern matching* in the later chapter. | ||
|
||
It's common to use tuple to return multiple values from a function. | ||
|
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
File renamed without changes.
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
File renamed without changes.
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
File renamed without changes.
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
7 changes: 4 additions & 3 deletions
7
moonbit-tour/tour/chapter2_pattern_matching/lesson7_or_pattern/index.md
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# Or Pattern | ||
# Or Pattern | ||
|
||
It's a little verbose if any two cases have common data and same way to handle them. For example, here is a enum `RGB` and a function `get_green` to get the green value from it. | ||
It's a little verbose if any two cases have common data and same way to handle them. For example, here is a enum `RGB` and a function `get_green` to get the green value from it. | ||
|
||
The `RGB` and `RGBA` cases can be combined as well. In an *or pattern*, the sub-patterns can introduce new variables, but they must be of the same type and have the same name in all sub-patterns. This restriction allows us to handle them uniformly. | ||
|
||
The `RGB` and `RGBA` cases can be combined as well. In an _or pattern_, the sub-patterns can introduce new variables, but they must be of the same type and have the same name in all sub-patterns. This restriction allows us to handle them uniformly. |