From b73f5278840400468e8e660288d94f53457860ec Mon Sep 17 00:00:00 2001 From: skylee03 <1178715749@qq.com> Date: Mon, 3 Jun 2024 16:29:43 +0800 Subject: [PATCH] Adjust structure --- course9/course_en.md | 24 ++++++++++++------------ course9/lecture_en.md | 20 ++++++++++---------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/course9/course_en.md b/course9/course_en.md index 026a3e1..73a8c59 100644 --- a/course9/course_en.md +++ b/course9/course_en.md @@ -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()` 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 `::(, ...)`, we can as well call the method using `.(...)`, given `` is of type ``. @@ -207,18 +219,6 @@ fn init { --- -# Automatic Derivation of Builtin Traits - -- Some simple builtin traits can be automatically derived by adding `derive()` 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. diff --git a/course9/lecture_en.md b/course9/lecture_en.md index db610db..802c1c8 100644 --- a/course9/lecture_en.md +++ b/course9/lecture_en.md @@ -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()` 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 `::(, ...)`, we can as well call the method using `.(...)`, given `` is of type ``. When defining such methods, `::` can also be omitted when the first parameter is named `self`. @@ -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()` 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.