Skip to content

Commit

Permalink
Adjust structure
Browse files Browse the repository at this point in the history
  • Loading branch information
skylee03 committed Jun 3, 2024
1 parent ae7e3d8 commit b73f527
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
24 changes: 12 additions & 12 deletions course9/course_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ impl MyTrait for Int with f(self) {

---

# Automatic Derivation of Builtin Traits

- Some simple builtin traits can be automatically derived by adding `derive(<traits>)` after the type definition.

```moonbit no-check
struct BoxedInt { value : Int } derive(Default, Eq, Compare, Debug)
```

- The member data types should have implemented the same traits.

---

# Method Chaining

- In addition to `<type>::<method>(<expr>, ...)`, we can as well call the method using `<expr>.<method>(...)`, given `<expr>` is of type `<type>`.
Expand All @@ -207,18 +219,6 @@ fn init {

---

# Automatic Derivation of Builtin Traits

- Some simple builtin traits can be automatically derived by adding `derive(<traits>)` after the type definition.

```moonbit no-check
struct BoxedInt { value : Int } derive(Default, Eq, Compare, Debug)
```

- The member data types should have implemented the same traits.

---

# Using Traits to Implement a Map

- A map is a collection of key-value pairs.
Expand Down
20 changes: 10 additions & 10 deletions course9/lecture_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ impl MyTrait for Int with f(self) {
}
```

### Automatic Derivation

In MoonBit, it is possible to automatically derive certain basic traits by adding `derive(<traits>)` after the type declaration. If the type is composed of other types, those member types must have already implemented the same traits.

In the given example, we derive the `Default`, `Eq`, `Compare`, and `Debug` traits for `BoxedInt`. These traits can be successfully derived because `Int` already implements them. However, if the required traits are not implemented by the member types, the compiler will generate an error message.

```moonbit no-check
struct BoxedInt { value : Int } derive(Default, Eq, Compare, Debug)
```

## Method Chaining

In addition to `<type>::<method>(<expr>, ...)`, we can as well call the method using `<expr>.<method>(...)`, given `<expr>` is of type `<type>`. When defining such methods, `<type>::` can also be omitted when the first parameter is named `self`.
Expand All @@ -176,16 +186,6 @@ fn init {
}
```

## Automatic Derivation of Builtin Traits

In MoonBit, it is possible to automatically derive certain basic traits by adding `derive(<traits>)` after the type declaration. If the type is composed of other types, those member types must have already implemented the same traits.

In the given example, we derive the `Default`, `Eq`, `Compare`, and `Debug` traits for `BoxedInt`. These traits can be successfully derived because `Int` already implements them. However, if the required traits are not implemented by the member types, the compiler will generate an error message.

```moonbit no-check
struct BoxedInt { value : Int } derive(Default, Eq, Compare, Debug)
```

## Example: Using Traits to Implement a Map

Now, let's proceed to implement a generic map using traits.
Expand Down

0 comments on commit b73f527

Please sign in to comment.