From 4530fe3ad3f0488a6c5c027025a3bdc20b5e5fbe Mon Sep 17 00:00:00 2001 From: Yorkin Date: Wed, 25 Dec 2024 17:20:57 +0800 Subject: [PATCH] tour: add lesson for newtype (#384) --- .../tour/chapter2_data_types/lesson4_newtype/index.mbt | 5 +++-- .../tour/chapter2_data_types/lesson4_newtype/index.md | 6 ++++++ .../{lesson4_option => lesson5_option}/index.mbt | 0 .../{lesson4_option => lesson5_option}/index.md | 0 .../{lesson5_result => lesson6_result}/index.mbt | 0 .../{lesson5_result => lesson6_result}/index.md | 0 6 files changed, 9 insertions(+), 2 deletions(-) rename moonbit-tour/tour/chapter2_data_types/{lesson4_option => lesson5_option}/index.mbt (100%) rename moonbit-tour/tour/chapter2_data_types/{lesson4_option => lesson5_option}/index.md (100%) rename moonbit-tour/tour/chapter2_data_types/{lesson5_result => lesson6_result}/index.mbt (100%) rename moonbit-tour/tour/chapter2_data_types/{lesson5_result => lesson6_result}/index.md (100%) diff --git a/moonbit-tour/tour/chapter2_data_types/lesson4_newtype/index.mbt b/moonbit-tour/tour/chapter2_data_types/lesson4_newtype/index.mbt index 3fc4a2de..717137ad 100644 --- a/moonbit-tour/tour/chapter2_data_types/lesson4_newtype/index.mbt +++ b/moonbit-tour/tour/chapter2_data_types/lesson4_newtype/index.mbt @@ -4,8 +4,9 @@ type UserName String fn main { let user_id : UserId = UserId(1) let user_name : UserName = UserName("Alice") - let UserId(id) = user_id - let UserName(name) = user_name println(user_id._) println(user_name._) + // use some pattern matching to extract the values + let UserId(id) = user_id + let UserName(name) = user_name } diff --git a/moonbit-tour/tour/chapter2_data_types/lesson4_newtype/index.md b/moonbit-tour/tour/chapter2_data_types/lesson4_newtype/index.md index e69de29b..7143fd9c 100644 --- a/moonbit-tour/tour/chapter2_data_types/lesson4_newtype/index.md +++ b/moonbit-tour/tour/chapter2_data_types/lesson4_newtype/index.md @@ -0,0 +1,6 @@ +# Newtype + +Newtypes are similar to enums with only one constructor (with the same name as the newtype itself). You can use the constructor to create values of the newtype and use `._` to extract the internal representation. + +*Pattern matching* can also be used. + diff --git a/moonbit-tour/tour/chapter2_data_types/lesson4_option/index.mbt b/moonbit-tour/tour/chapter2_data_types/lesson5_option/index.mbt similarity index 100% rename from moonbit-tour/tour/chapter2_data_types/lesson4_option/index.mbt rename to moonbit-tour/tour/chapter2_data_types/lesson5_option/index.mbt diff --git a/moonbit-tour/tour/chapter2_data_types/lesson4_option/index.md b/moonbit-tour/tour/chapter2_data_types/lesson5_option/index.md similarity index 100% rename from moonbit-tour/tour/chapter2_data_types/lesson4_option/index.md rename to moonbit-tour/tour/chapter2_data_types/lesson5_option/index.md diff --git a/moonbit-tour/tour/chapter2_data_types/lesson5_result/index.mbt b/moonbit-tour/tour/chapter2_data_types/lesson6_result/index.mbt similarity index 100% rename from moonbit-tour/tour/chapter2_data_types/lesson5_result/index.mbt rename to moonbit-tour/tour/chapter2_data_types/lesson6_result/index.mbt diff --git a/moonbit-tour/tour/chapter2_data_types/lesson5_result/index.md b/moonbit-tour/tour/chapter2_data_types/lesson6_result/index.md similarity index 100% rename from moonbit-tour/tour/chapter2_data_types/lesson5_result/index.md rename to moonbit-tour/tour/chapter2_data_types/lesson6_result/index.md