("#locales")!;
4 |
5 | export function init() {
6 | localeButton.addEventListener("mouseenter", () => {
7 | locales.classList.remove("hidden");
8 | });
9 | localeButton.addEventListener("mouseleave", () => {
10 | locales.classList.add("hidden");
11 | });
12 | }
13 |
--------------------------------------------------------------------------------
/moonbit-tour/src/util.ts:
--------------------------------------------------------------------------------
1 | import type * as type from "./type";
2 |
3 | export function debounce(
4 | f: (...args: P) => R,
5 | timeout: number,
6 | ) {
7 | let timer: ReturnType | null = null;
8 | return (...args: P) => {
9 | if (timer !== null) {
10 | clearTimeout(timer);
11 | }
12 | timer = setTimeout(() => {
13 | f(...args);
14 | timer = null;
15 | }, timeout);
16 | };
17 | }
18 |
19 | export function getTheme(): type.Theme {
20 | return (localStorage.getItem("theme") as type.Theme) ?? "light";
21 | }
22 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson10_loop/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let array = [1, 2, 3]
3 |
4 | println("for loop:")
5 | for i = 0; i < array.length(); i = i + 1 {
6 | println("array[\{i}]: \{array[i]}")
7 | }
8 |
9 | println("\nfunctional for loop:")
10 |
11 | let sum = for i = 1, acc = 0; i <= 10; i = i + 1 {
12 | let even = i % 2 == 0
13 | continue i + 1, acc + i
14 | } else {
15 | acc
16 | }
17 | println(sum)
18 |
19 | println("\nwhile loop:")
20 | let mut j = 0
21 | while true {
22 | println("array[\{j}]: \{array[j]}")
23 | j = j + 1
24 | if j < array.length() {
25 | continue
26 | } else {
27 | break
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson11_for_in_loop/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | println("for-in loop:")
3 | let array = [1, 2, 3]
4 | for element in array {
5 | println("element: \{element}")
6 | }
7 |
8 | println("for-in loop with index:")
9 | for i, element in array {
10 | println("index: \{i}, element: \{element}")
11 | }
12 |
13 | println("for-in loop for map:")
14 | let map = { "key1": 1, "key2": 2, "key3": 3 }
15 | for k, v in map {
16 | println("key: \{k}, value: \{v}")
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson12_range/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | println("number 1 to 3:")
3 | for i in 1..<4 {
4 | println(i)
5 | }
6 |
7 | println("number 1 to 4:")
8 | for i in 1..=4 {
9 | println(i)
10 | }
11 | }
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson12_range/index.md:
--------------------------------------------------------------------------------
1 | # Range
2 |
3 | You can also use *range* in a for-in loop.
4 |
5 | - `start.. Int {
19 | if n < 2 {
20 | n
21 | } else {
22 | fib(n - 1) + fib(n - 2)
23 | }
24 | }
25 |
26 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson1_variable/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let a : Int = 10
3 | let b = 20
4 | println(a + b)
5 |
6 | let mut c = 10
7 | c = c + 1
8 | println(c)
9 |
10 | let d = 20
11 | // d = d + 1
12 | println(d)
13 | }
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson1_variable/index.md:
--------------------------------------------------------------------------------
1 | # Variables
2 |
3 | The `let` keyword is used to define a variable.
4 |
5 | The type of the variable can be annotated by using a colon followed by the type.
6 | It is optional; if not provided, the type will be inferred from the value.
7 |
8 | Variables are immutable by default in MoonBit. You can add an extra `mut`
9 | keyword to make them mutable at the local level.
10 |
11 | If you uncomment the `d = d + 1`, you will get an error.
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson2_numbers/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let dec : Int = 1000000
3 | let dec2 : Int = 1_000_000
4 | let hex : Int = 0xFFFF
5 | let oct = 0o777
6 | let bin = 0b1001
7 |
8 | println(1 + 2)
9 | println(1 - 2)
10 | println(1 * 2)
11 | println(5 / 2)
12 | println(10 % 3)
13 |
14 | let num1 : Double = 3.14
15 | let num2 : Float = 3.14
16 | }
17 |
18 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson2_numbers/index.md:
--------------------------------------------------------------------------------
1 | # Numbers
2 |
3 | Integers and floats are the most common types in MoonBit.
4 | `Int`(32 bit signed integer) can be represented in decimal, hexadecimal, octal, and binary,
5 | and you can use the underscore to separate digits for better readability.
6 | We call these *number literals*.
7 |
8 | The `0xFFFF` is a hexadecimal number, `0o777` is an octal number, `0b1010` is a binary number,
9 | and `1_000_000` is a decimal number equivalent to `1000000`.
10 |
11 |
12 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson3_function/index.mbt:
--------------------------------------------------------------------------------
1 | fn add(a : Int, b : Int) -> Int {
2 | return a + b
3 | }
4 |
5 | fn compute() -> Unit {
6 | println(add(2, 40))
7 | }
8 |
9 | fn main {
10 | compute()
11 | }
12 |
13 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson3_function/index.md:
--------------------------------------------------------------------------------
1 | # Function
2 |
3 | This example defines two functions, `add` and `compute`.
4 |
5 | The `add` function takes two arguments, `a` and `b`, and returns their sum.
6 |
7 | The `compute` function takes no arguments and returns nothing.
8 | Its return type is `Unit`, which is used to represent the absence of a return value.
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson4_block/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let a = 100
3 |
4 | {
5 | let mut a = 0
6 | println("checkpoint 1")
7 | a = a + 1
8 | }
9 |
10 | println("checkpoint 2")
11 | println(f())
12 | }
13 |
14 | fn f() -> Int {
15 | let b = 3.14
16 |
17 | let result = {
18 | let b = 1
19 | println("checkpoint 3")
20 | b + 5
21 | }
22 |
23 | result // same as `return result` here
24 | }
25 |
26 |
27 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson5_array/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let arr1 : Array[Int] = [1, 2, 3, 4, 5]
3 | let arr2 = Array::make(4,1)
4 |
5 | println(arr1.length()) // get the length of the array
6 | println(arr1[1]) // get the second element of the array
7 |
8 | // We can also use the spread operator to concatenate arrays.
9 | let arr3 = [..arr1, 1000, 2000, ..arr2, 3000, 4000]
10 | println("spread arrays:")
11 | println(arr3)
12 |
13 | let view : ArrayView[Int] = arr1[1:4]
14 | println("array view:")
15 | println(view)
16 | println("view[0]:")
17 | println(view[0])
18 |
19 | arr1.push(6) // push an element to the end
20 | println("updated array:")
21 | println(arr1)
22 | }
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson6_string/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let str = "Hello, World!"
3 | // Access a character by index.
4 | let c : Char = str[4]
5 | println(c)
6 | let c2 = 'o'
7 | println(c == c2)
8 |
9 | // Use escape sequence.
10 | println("\nHello, \tWorld!")
11 | println("unicode \u{1F407} is a rabbit")
12 |
13 | // Concatenate two strings.
14 | println(str + " Hello, MoonBit!")
15 |
16 | // Use string interpolation.
17 | let moon = "Moon"
18 | let bit = "Bit"
19 | println("Use \{moon + bit}. Happy coding")
20 | }
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson6_string/index.md:
--------------------------------------------------------------------------------
1 | # String
2 |
3 | A string is a sequence of characters encoded in UTF-16. In MoonBit, strings are immutable,
4 | which means you cannot change the elements inside a string.
5 |
6 | MoonBit supports C-style escape characters in strings and chars, such as `\n` (newline),
7 | `\t` (tab), `\\` (backslash), `\"` (double-quote), and `\'` (single-quote).
8 |
9 | Unicode escape characters are also supported. You can use `\u{...}` (where `...` represents
10 | the Unicode character's hex code) to represent a Unicode character by its code point.
11 |
12 | MoonBit also supports string interpolation written like `\{variable}`, which allows you to embed expressions into strings.
13 |
14 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson7_tuple/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | // create Tuple
3 | let tuple = (3.14, false, [1,2,3])
4 | let tuple2 : (Float, Bool, Int) = (2.1, true, 20)
5 | println(tuple)
6 |
7 | // Accessing tuple elements
8 | println(tuple.0)
9 | println(tuple.2)
10 |
11 | // Tuple can also be destructured.
12 | let (a, b, c) = f()
13 | println("\{a}, \{b}, \{c}")
14 | }
15 |
16 | fn f() -> (Int, Bool, Double) {
17 | (1, false, 3.14) // return multiple values via tuple
18 | }
19 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson7_tuple/index.md:
--------------------------------------------------------------------------------
1 | # Tuple
2 |
3 | A tuple is a collection of values that can have different types. It is immutable,
4 | which means that once it is created, it cannot be changed. It is created using
5 | parentheses.
6 |
7 | You can access the elements of tuple via the index: `tuple.0`, `tuple.1`, etc.
8 |
9 | A tuple can be destructed via syntax like `let (a,b) = tuple`, where the `tuple` on
10 | the right side is a tuple with two elements, and `a` and `b` are the variables to
11 | store the elements. This is a special use case of *pattern matching* which we will
12 | introduce in a later chapter.
13 |
14 | It's common to use a tuple to return multiple values from a function.
15 |
16 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson8_map/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | // Create a map by map literal
3 | let map1 = { "key1": 1, "key2": 2, "key3": 3 }
4 | println(map1)
5 | // You can also create a map by Map::of, from a list of key-value pairs
6 | let map2 = Map::of([("key1", 1), ("key2", 2), ("key3", 3)])
7 | println(map1 == map2)
8 |
9 | // Access a value by key
10 | println(map1.get("key1"))
11 |
12 | // Update a value by key
13 | map1["key1"] = 10
14 | println(map1)
15 |
16 | // test a if a key exists
17 | println(map1.contains("key1"))
18 | }
19 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson9_if_else/index.mbt:
--------------------------------------------------------------------------------
1 | fn fib(x : Int) -> Int {
2 | if x < 2 {
3 | x
4 | } else {
5 | fib(x - 1) + fib(x - 2)
6 | }
7 | }
8 |
9 | fn main {
10 | if 5 > 1 {
11 | println("5 is greater than 1")
12 | }
13 | println(fib(5))
14 | println(weekday(3))
15 | }
16 |
17 | fn weekday(x : Int) -> String {
18 | if x == 1 {
19 | "Monday"
20 | } else if x == 2 {
21 | "Tuesday"
22 | } else if x == 3 {
23 | "Wednesday"
24 | } else if x == 4 {
25 | "Thursday"
26 | } else if x == 5 {
27 | "Friday"
28 | } else if x == 6 {
29 | "Saturday"
30 | } else if x == 7 {
31 | "Sunday"
32 | } else {
33 | "Invalid day"
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter1_basics/lesson9_if_else/index.md:
--------------------------------------------------------------------------------
1 | # If expression
2 |
3 | An if expression is a conditional control flow expression that has a result value.
4 |
5 | In an if expression, each branch must have the same type. If the condition is true, it returns the result value of the first branch. Otherwise, it returns the result value of the second branch.
6 |
7 | The `else` part is optional. If it is omitted, the type of the whole if expression will be `Unit`.
8 |
9 | Nested if expressions in the else part can be shortened by using `else if`.
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter2_data_types/lesson1_struct/index.mbt:
--------------------------------------------------------------------------------
1 | struct Point {
2 | x : Int
3 | y : Int
4 | } derive(Show)
5 |
6 | fn main {
7 | // create a point
8 | let point = { x: 3, y: 4 }
9 | println("point: \{point}")
10 | println("point.x: \{point.x}")
11 | println("point.y: \{point.y}")
12 |
13 | // functional update
14 | let point2 = {..point, x: 20}
15 | println(point2)
16 | }
17 |
18 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter2_data_types/lesson2_mutable_field/index.mbt:
--------------------------------------------------------------------------------
1 | struct MutPoint {
2 | mut mx : Int
3 | y : Int
4 | } derive(Show)
5 |
6 | fn main {
7 | let point = { mx: 3, y: 4 }
8 | println("point: \{point}")
9 | point.mx = 10
10 | println("point: \{point}")
11 | }
12 |
13 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter2_data_types/lesson2_mutable_field/index.md:
--------------------------------------------------------------------------------
1 | # Mutable fields in Struct
2 |
3 | Struct fields are immutable by default, but we can make them mutable by using the `mut` keyword in the field declaration.
4 |
5 | In previous lessons, we have learned that collections in MoonBit can be either mutable or immutable. This is achieved by using the `mut` keyword in their type declaration.
6 |
7 | The `MutPoint` struct in the example has two fields, mutable `mx` and immutable `y`.
8 | You can change the value of the `mx` field via reassignment but not the value of `y`.
9 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter2_data_types/lesson4_newtype/index.mbt:
--------------------------------------------------------------------------------
1 | type UserId Int derive(Show)
2 | type UserName String derive(Show)
3 |
4 | fn main {
5 | let user_id : UserId = UserId(1)
6 | let user_name : UserName = UserName("Alice")
7 | println(user_id._)
8 | println(user_name._)
9 | // use some pattern matching to extract the values
10 | let UserId(id) = user_id
11 | let UserName(name) = user_name
12 | }
13 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter2_data_types/lesson4_newtype/index.md:
--------------------------------------------------------------------------------
1 | # Newtype
2 |
3 | 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.
4 |
5 | You can also use *pattern matching* with newtypes.
6 |
7 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter2_data_types/lesson5_option/index.mbt:
--------------------------------------------------------------------------------
1 | fn first_char(s : String) -> Option[Char] {
2 | if s.length() == 0 {
3 | None
4 | } else {
5 | Some(s[0])
6 | }
7 | }
8 |
9 | fn main {
10 | let c1 : Char? = first_char("hello")
11 | let c2 : Option[Char] = first_char("")
12 | println("\{c1.is_empty()}, \{c1.unwrap()}")
13 | println("\{c2.is_empty()}, \{c2}")
14 | }
15 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter2_data_types/lesson5_option/index.md:
--------------------------------------------------------------------------------
1 | # Option
2 |
3 | `Option[Char]` is an enum that represents a `Char` value that may or may not be present. It is a common way to handle exceptional cases.
4 |
5 | - `None` means the value is missing.
6 | - `Some(e)` is a wrapper that contains the value `e`.
7 |
8 | The `[Char]` part in the type is a type parameter, which means the value type in `Option` is `Char`. We can use `Option[String]`, `Option[Double]`, etc. We will cover generics later.
9 |
10 | The type annotation `Option[A]` can be shortened to `A?`.
11 |
12 | You can use `c1.is_empty()` to check if the value is missing and `c1.unwrap()` to get the value.
13 |
14 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter2_data_types/lesson6_result/index.mbt:
--------------------------------------------------------------------------------
1 | fn first_char(s : String) -> Result[Char, String] {
2 | if s.length() == 0 {
3 | Err("empty string")
4 | } else {
5 | Ok(s[0])
6 | }
7 | }
8 |
9 | fn main {
10 | let c1 = first_char("hello")
11 | let c2 = first_char("")
12 | println("\{c1.is_ok()}, \{c1}, \{c1.unwrap()}")
13 | println("\{c2.is_err()}, \{c2}")
14 | }
15 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter2_data_types/lesson6_result/index.md:
--------------------------------------------------------------------------------
1 | # Result
2 |
3 | Similar to `Option[Char]`, the enum `Result[Char, String]` represents a `Char` value that may or may not be present. If not present, it can contain an error message of type `String`.
4 |
5 | - `Err("error message")` means the value is missing, and the error message is provided.
6 | - `Ok('h')` is a wrapper that contains the value `'h'`.
7 |
8 | 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.
9 |
10 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter3_pattern_matching/lesson1_introduction/index.mbt:
--------------------------------------------------------------------------------
1 | struct Point {
2 | x : Int
3 | y : Int
4 | } derive(Show)
5 |
6 | fn main {
7 | let tuple = (1, false, 3.14)
8 | let (a, b, c) = tuple
9 | println("a:\{a}, b:\{b}, c:\{c}")
10 | let record = { x: 5, y: 6 }
11 | let { x, y } = record
12 | println("x:\{x}, y:\{y}")
13 | }
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter3_pattern_matching/lesson1_introduction/index.md:
--------------------------------------------------------------------------------
1 | # Pattern Matching
2 |
3 | We have seen pattern matching in the previous example.
4 | It's a powerful feature in MoonBit that can be used in many places. It can help you test conditions conveniently and effectively, making your programs more precise and robust.
5 |
6 | In this example, we give some basic use cases of pattern matching. Some other languages call it "destructuring" or "structured bindings", a way to extract values from a complex data structure.
7 |
8 | "Destructuring" is just a subset of this feature.
9 | In MoonBit, almost every type you can construct can have a form to "destruct", which we call a *pattern*.
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter3_pattern_matching/lesson2_patterns_in_let/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | f([1, 2, 3])
3 | f([1, 2])
4 | }
5 |
6 | fn f(array : Array[Int]) -> Unit {
7 | let [a, b, c] = array // warning as error
8 | println("a:\{a}, b:\{b}, c:\{c}")
9 | }
10 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter3_pattern_matching/lesson4_constant_pattern/index.md:
--------------------------------------------------------------------------------
1 | # Constant pattern
2 |
3 | Almost all constants in MoonBit can be represented as a constant pattern.
4 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter3_pattern_matching/lesson5_tuple_pattern/index.mbt:
--------------------------------------------------------------------------------
1 | fn logical_and(x : Bool, y : Bool) -> Bool {
2 | match (x, y) {
3 | (true, true) => true
4 | (false, _) => false
5 | (_, false) => false
6 | }
7 | }
8 |
9 | fn logical_or(x : Bool, y : Bool) -> Bool {
10 | match (x, y) {
11 | (true, _) => true
12 | (_, true) => true
13 | _ => false
14 | }
15 | }
16 |
17 | fn main {
18 | println("true and false: \{logical_and(true, false)}")
19 | println("true or false: \{logical_or(true, false)}")
20 | }
21 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter3_pattern_matching/lesson5_tuple_pattern/index.md:
--------------------------------------------------------------------------------
1 | # Tuple pattern
2 |
3 | Use a tuple pattern to match multiple conditions at once.
4 |
5 | This example simulates *logical and* and *logical or* operations via pattern matching.
6 |
7 | In this scenario, the overhead of creating the tuple in the condition will be optimized out by the compiler.
8 |
9 |
10 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter3_pattern_matching/lesson6_alias_pattern/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let (a, (b, _) as tuple, _) as triple = (1, (true, 5), false)
3 | println("a: \{a}, b: \{b}")
4 | println("tuple: \{tuple}")
5 | println("triple: \{triple}")
6 | }
7 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter3_pattern_matching/lesson6_alias_pattern/index.md:
--------------------------------------------------------------------------------
1 | # Alias Pattern
2 |
3 | Any pattern can be bound to an extra new name via an *alias pattern*. The syntax is `pattern as name`. In this example, we use this feature to preserve the original tuples while pattern matching them.
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter3_pattern_matching/lesson7_array_pattern/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let array = [1, 2, 3, 4, 5, 6]
3 | match array {
4 | [a, b, ..] => println("a: \{a}, b: \{b}")
5 | _ => ()
6 | }
7 | match array {
8 | [.., c, d] => println("c: \{c}, d: \{d}")
9 | _ => ()
10 | }
11 | match array {
12 | [e, .., f] => println("e: \{e}, f: \{f}")
13 | _ => ()
14 | }
15 | println("sum of array: \{sum(array)}")
16 | }
17 |
18 | fn sum(array : @array.View[Int]) -> Int {
19 | match array {
20 | [] => 0
21 | [x, .. xs] => x + sum(xs)
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter3_pattern_matching/lesson7_array_pattern/index.md:
--------------------------------------------------------------------------------
1 | # Array Pattern
2 |
3 | An array pattern is a sequence of patterns enclosed in `[]` that matches an array.
4 |
5 | You can use `..` to match the rest of the array at the start, end, or middle elements of the array.
6 |
7 | In an array pattern, the `..` part can be bound to a new variable via an *alias pattern*. The type of that variable is `@array.View`. The `sum` function uses this feature to calculate the sum of the array recursively.
8 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter3_pattern_matching/lesson8_or_pattern/index.mbt:
--------------------------------------------------------------------------------
1 | enum Color {
2 | Blue
3 | Red
4 | Green
5 | RGB(Int, Int, Int)
6 | RGBA(Int, Int, Int, Int)
7 | } derive(Show)
8 |
9 | fn get_green(color : Color) -> Int {
10 | match color {
11 | Blue | Red => 0
12 | Green => 255
13 | RGB(_, g, _) | RGBA(_, g, _, _) => g
14 | }
15 | }
16 |
17 | fn main {
18 | println("The green part of Red is \{get_green(Red)}")
19 | println("The green part of Green is \{get_green(Green)}")
20 | println("The green part of Blue is \{get_green(Blue)}")
21 | println("The green part of RGB(0,0,0) is \{get_green(RGB(0,0,0))}")
22 | println("The green part of RGBA(50,5,0,6) is \{get_green(RGBA(50,5,0,6))}")
23 | }
24 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter3_pattern_matching/lesson8_or_pattern/index.md:
--------------------------------------------------------------------------------
1 | # Or Pattern
2 |
3 | It's a little verbose if any two cases have common data and the same code to handle them. For example, here is an enum `RGB` and a function `get_green` to get the green value from it.
4 |
5 | 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.
6 |
7 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter3_pattern_matching/lesson9_range_pattern/index.md:
--------------------------------------------------------------------------------
1 | # Range Pattern
2 |
3 | For consecutive values, using the previously introduced *or-patterns* can be somewhat cumbersome. To address this issue, we can use *range patterns*. *Range patterns* can match values within a specified range.
4 |
5 |
6 | Recall the syntax we learned in Chapter 1:
7 |
8 | - `start.. UInt {
2 | match list {
3 | Nil => 0
4 | Cons(_, rest) => count(rest) + 1 // may overflow
5 | }
6 | }
7 |
8 | ///|
9 | fn main {
10 | let empty_list : @immut/list.T[Int] = Nil
11 | let list_1 = @immut/list.Cons(1, empty_list)
12 | let list_2 = @immut/list.Cons(2, list_1)
13 | let list_3 = @immut/list.Cons(3, empty_list)
14 | let reversed_1 = list_1.rev()
15 | let n = count(reversed_1)
16 | }
17 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter4_more_data_types/lesson1_immutable_list/index.md:
--------------------------------------------------------------------------------
1 | # Immutable List
2 |
3 | The immutable list resides in the package `@immut/list`.
4 |
5 | It is either:
6 |
7 | - `Nil` : an empty list
8 | - `Cons` : an element and the rest of the list.
9 |
10 |
12 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter4_more_data_types/lesson2_immutable_set/index.mbt:
--------------------------------------------------------------------------------
1 | ///|
2 | fn main {
3 | let a = @immut/hashset.of([5, 4, 3, 2, 1])
4 | let b = @immut/sorted_set.of([5, 4, 3, 2, 1])
5 | let arraya = a.iter().collect()
6 | let arrayb = b.iter().collect()
7 | let c = a.add(6)
8 | let d = b.add(6)
9 | let except_one = d.remove_min()
10 | println(d)
11 | }
12 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter4_more_data_types/lesson2_immutable_set/index.md:
--------------------------------------------------------------------------------
1 | # Immutable Set
2 |
3 | The immutable set has two variants:
4 |
5 | - Hash set in the `@immut/hashset`
6 | - Sorted set in the `@immut/sorted_set`
7 |
8 | The first one is hash based and hence unordered while the second one require its key
9 | to be ordered.
10 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter4_more_data_types/lesson3_immutable_map/index.mbt:
--------------------------------------------------------------------------------
1 | ///|
2 | fn main {
3 | let a = @immut/hashmap.of([(1, "a"), (2, "b"), (3, "c")])
4 | let b = a.add(4, "d")
5 | let c = @immut/sorted_map.from_iter(b.iter())
6 | println(c.keys())
7 | }
8 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter4_more_data_types/lesson3_immutable_map/index.md:
--------------------------------------------------------------------------------
1 | # Immutable Map
2 |
3 | Similar to the immutable set, there are two immutable maps:
4 |
5 | - Hash map in the `@immut/hashmap`
6 | - Sorted map in the `@immut/sorted_map`
7 |
8 | The first one is hash based and hence unordered while the second one require its
9 | key to be ordered.
--------------------------------------------------------------------------------
/moonbit-tour/tour/chapter5_methods_and_traits/lesson1_methods/index.mbt:
--------------------------------------------------------------------------------
1 | type MyInt Int
2 |
3 | fn increment(self : MyInt) -> MyInt {
4 | MyInt(self._ + 1)
5 | }
6 |
7 | fn MyInt::println(x : MyInt) -> Unit {
8 | println("MyInt(\{x._})")
9 | }
10 |
11 | fn main {
12 | let x = MyInt(39)
13 | let y = x.increment() // call method via dot syntax
14 | let z = increment(y) // `fn increment(..)` can be called directly
15 | let w = MyInt::increment(z) // call methods using qualified syntax
16 | w.println()
17 | MyInt::println(w) // `fn MyInt::println` cannot be called via `println(..)` directly
18 | }
19 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | println("hello")
3 | }
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson10_loop/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let array = [1, 2, 3]
3 |
4 | println("for loop:")
5 | for i = 0; i < array.length(); i = i + 1 {
6 | println("array[\{i}]: \{array[i]}")
7 | }
8 |
9 | println("\nfunctional for loop:")
10 |
11 | let sum = for i = 1, acc = 0; i <= 10; i = i + 1 {
12 | let even = i % 2 == 0
13 | continue i + 1, acc + i
14 | } else {
15 | acc
16 | }
17 | println(sum)
18 |
19 | println("\nwhile loop:")
20 | let mut j = 0
21 | while true {
22 | println("array[\{j}]: \{array[j]}")
23 | j = j + 1
24 | if j < array.length() {
25 | continue
26 | } else {
27 | break
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson10_loop/index.md:
--------------------------------------------------------------------------------
1 | # 循环
2 |
3 | 本示例展示了使用 for 循环和 while 循环遍历数组。
4 |
5 | ## for 循环表达式
6 |
7 | for 循环类似于 C 风格的 for 循环:
8 |
9 | ```
10 | for 初始化; 条件; 增量 {
11 | // 循环体
12 | }
13 | ```
14 |
15 | 循环开始前执行初始化部分。每次迭代时:
16 | 1. 检查条件表达式
17 | 2. 若条件为真则执行循环体
18 | 3. 执行增量表达式
19 | 4. 重复上述步骤直到条件为假
20 |
21 | MoonBit 的 for 循环比 C 风格更灵活,后续章节将详细说明。
22 |
23 | ## while 循环表达式
24 |
25 | while 循环同样类似 C 风格:
26 |
27 | ```
28 | while 条件 {
29 | // 循环体
30 | }
31 | ```
32 |
33 | 执行流程:
34 | 1. 检查条件表达式
35 | 2. 若条件为真则执行循环体
36 | 3. 重复上述步骤直到条件为假
37 |
38 | MoonBit 也支持在循环中使用`continue`和`break`语句。
39 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson11_for_in_loop/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | println("for-in loop:")
3 | let array = [1, 2, 3]
4 | for element in array {
5 | println("element: \{element}")
6 | }
7 |
8 | println("for-in loop with index:")
9 | for i, element in array {
10 | println("index: \{i}, element: \{element}")
11 | }
12 |
13 | println("for-in loop for map:")
14 | let map = { "key1": 1, "key2": 2, "key3": 3 }
15 | for k, v in map {
16 | println("key: \{k}, value: \{v}")
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson11_for_in_loop/index.md:
--------------------------------------------------------------------------------
1 | # For-in 循环
2 |
3 | 手动编写 for 循环并决定结束条件较为繁琐。
4 |
5 | 若需遍历集合,可使用 `for .. in ... {}` 循环。
6 |
7 | 第一个 for-in 循环遍历数组,每次迭代会将元素绑定至变量 `element`。
8 |
9 | 第二个循环遍历键值对映射(Map),将键绑定至第一个变量 (`k`),值绑定至第二个变量 (`v`)。
10 |
11 | 哪些集合支持 for-in 循环?何时支持双变量遍历?这取决于集合的 API 设计:
12 |
13 | - 若集合提供 `iter()` 方法返回 `Iter[V]` 迭代器,则支持单变量遍历
14 | - 若集合提供 `iter2()` 方法返回 `Iter2[K,V]` 迭代器,则支持双变量遍历
15 |
16 | 我们将在后续章节详解迭代器机制。
17 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson12_range/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | println("number 1 to 3:")
3 | for i in 1..<4 {
4 | println(i)
5 | }
6 |
7 | println("number 1 to 4:")
8 | for i in 1..=4 {
9 | println(i)
10 | }
11 | }
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson12_range/index.md:
--------------------------------------------------------------------------------
1 | # 范围表达式
2 |
3 | 可在 for-in 循环中使用*范围表达式*:
4 |
5 | - `start.. Int {
19 | if n < 2 {
20 | n
21 | } else {
22 | fib(n - 1) + fib(n - 2)
23 | }
24 | }
25 |
26 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson14_test/index.md:
--------------------------------------------------------------------------------
1 | # 测试
2 |
3 | MoonBit 内置测试支持,无需导入或配置额外包和工具。只需使用测试块并在其中编写测试代码。
4 |
5 | **注意:本教程暂不支持此功能。您可以在我们的 [playground](https://try.moonbitlang.com) 或安装 MoonBit 工具链后在终端中尝试。**
6 |
7 | 在第一个测试块中,我们使用内置函数 `assert_eq`、`assert_false` 和 `assert_true` 测试一些属性。通过在终端运行 `moon test` 或点击集成开发环境 (IDE) 中的测试按钮,即可执行测试。
8 |
9 | ## 维护测试
10 |
11 | 手动维护预期值有时很繁琐。MoonBit 也支持内置的*快照测试*。快照测试会运行被测代码并将预期结果存储为快照。
12 |
13 | 在第二个测试块中,我们使用 `inspect` 函数测试 `fib` 的结果和数组的 `map` 方法。通过在终端运行 `moon test --update` 或点击 IDE 中的 `Update test` 按钮,结果将自动插入为第二个参数。
14 |
15 | 下次运行测试时,它将报告当前结果与存储结果之间的差异。您可以使用 `--update` 标志将存储结果更新为新结果。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson1_variable/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let a : Int = 10
3 | let b = 20
4 | println(a + b)
5 |
6 | let mut c = 10
7 | c = c + 1
8 | println(c)
9 |
10 | let d = 20
11 | // d = d + 1
12 | println(d)
13 | }
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson1_variable/index.md:
--------------------------------------------------------------------------------
1 | # 变量
2 |
3 | `let`关键字用于定义变量。
4 |
5 | 可通过在变量名后添加冒号和类型进行类型注解,此注解为可选项。当省略类型注解时,编译器会自动从赋值表达式推断变量类型。
6 |
7 | MoonBit 中变量默认不可变。如需创建可变变量,需额外添加`mut`关键字,该可变性仅作用于当前作用域。
8 |
9 | 若取消`d = d + 1`的注释,将触发编译错误。(注:因尝试修改不可变变量)
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson2_numbers/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let dec : Int = 1000000
3 | let dec2 : Int = 1_000_000
4 | let hex : Int = 0xFFFF
5 | let oct = 0o777
6 | let bin = 0b1001
7 |
8 | println(1 + 2)
9 | println(1 - 2)
10 | println(1 * 2)
11 | println(5 / 2)
12 | println(10 % 3)
13 |
14 | let num1 : Double = 3.14
15 | let num2 : Float = 3.14
16 | }
17 |
18 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson2_numbers/index.md:
--------------------------------------------------------------------------------
1 | # 数值类型
2 |
3 | 整型(Int)和浮点型(Float)是 MoonBit 中最基础的数值类型。
4 |
5 | 整型(32 位有符号整数)支持十进制、十六进制、八进制和二进制表示法,可使用下划线分隔数字以提升可读性。这些表示法统称为*数值字面量*。
6 |
7 | 示例说明:
8 | - `0xFFFF` 表示十六进制数
9 | - `0o777` 表示八进制数
10 | - `0b1010` 表示二进制数
11 | - `1_000_000` 等效于十进制数`1000000`(增强可读性写法)
12 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson3_function/index.mbt:
--------------------------------------------------------------------------------
1 | fn add(a : Int, b : Int) -> Int {
2 | return a + b
3 | }
4 |
5 | fn compute() -> Unit {
6 | println(add(2, 40))
7 | }
8 |
9 | fn main {
10 | compute()
11 | }
12 |
13 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson3_function/index.md:
--------------------------------------------------------------------------------
1 | # 函数
2 |
3 | 本示例定义了两个函数:`add`和`compute`。
4 |
5 | `add`函数接收两个参数`a`和`b`,返回它们的和。
6 |
7 | `compute`函数无参数且无返回值,其返回类型为`Unit`,该类型用于表示没有返回值的函数。
8 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson4_block/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let a = 100
3 |
4 | {
5 | let mut a = 0
6 | println("checkpoint 1")
7 | a = a + 1
8 | }
9 |
10 | println("checkpoint 2")
11 | println(f())
12 | }
13 |
14 | fn f() -> Int {
15 | let b = 3.14
16 |
17 | let result = {
18 | let b = 1
19 | println("checkpoint 3")
20 | b + 5
21 | }
22 |
23 | result // same as `return result` here
24 | }
25 |
26 |
27 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson4_block/index.md:
--------------------------------------------------------------------------------
1 | # 代码块与语句
2 |
3 | 代码块是由若干语句和可选结尾表达式组成的复合结构:
4 |
5 | ```
6 | {
7 | statement1
8 | statement2
9 | expression
10 | }
11 | ```
12 |
13 | 以上代码块将按顺序执行`statement1`、`statement2`,最后计算`expression`的值作为整个代码块的返回值。若省略结尾表达式,代码块将返回`()`(其类型为`Unit`)。
14 |
15 | 语句可以是以下形式:
16 | - 变量声明
17 | - 变量赋值
18 | - 任何返回`Unit`类型的表达式
19 |
20 | 代码块同时关联着命名空间作用域。在`main`函数示例中,内部代码块声明的变量`a`会遮蔽(shadow)外部同名变量`a`,且仅在内部代码块中可见。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson5_array/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let arr1 : Array[Int] = [1, 2, 3, 4, 5]
3 | let arr2 = Array::make(4,1)
4 |
5 | println(arr1.length()) // get the length of the array
6 | println(arr1[1]) // get the second element of the array
7 |
8 | // We can also use the spread operator to concatenate arrays.
9 | let arr3 = [..arr1, 1000, 2000, ..arr2, 3000, 4000]
10 | println("spread arrays:")
11 | println(arr3)
12 |
13 | let view : ArrayView[Int] = arr1[1:4]
14 | println("array view:")
15 | println(view)
16 | println("view[0]:")
17 | println(view[0])
18 |
19 | arr1.push(6) // push an element to the end
20 | println("updated array:")
21 | println(arr1)
22 | }
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson5_array/index.md:
--------------------------------------------------------------------------------
1 | # 数组
2 |
3 | 数组是存储相同类型元素的集合。
4 |
5 | 可通过*数组字面量语法*创建数组,即用方括号包裹逗号分隔的元素列表:`[1,2,3]`。
6 |
7 | 也可使用`Array::make`函数创建数组,该函数接收长度和元素值作为参数。如示例中`Array::make(4,1)`将创建等同于`[1,1,1,1]`的数组。
8 |
9 | `arr3`示例展示了由`arr1`元素、`arr2`元素及额外数字组成的数组。方括号中的`..arr1`语法称为*数组展开*(array spread),用于将数组元素展开到新数组中。
10 |
11 | ## 数组视图
12 |
13 | 使用`array[start:end]`语法可获取数组的视图(索引范围包含 start 但不包含 end)。`start`和`end`参数可省略。视图是原数组的引用,可避免数组复制。
14 |
15 | ## 数组可变性
16 |
17 | 可能注意到我们对未标记`mut`的数组`arr1`执行了`push`操作。这是因为数组元素的 mutability 由**数组类型自身决定**。`let`声明中的`mut`关键字仅控制变量名能否被重新赋值。
18 |
19 | 若尝试将`arr1`重新赋值为其他数组(如`arr1 = [1,2,3]`),将触发编译错误。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson6_string/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let str = "Hello, World!"
3 | // Access a character by index.
4 | let c : Char = str[4]
5 | println(c)
6 | let c2 = 'o'
7 | println(c == c2)
8 |
9 | // Use escape sequence.
10 | println("\nHello, \tWorld!")
11 | println("unicode \u{1F407} is a rabbit")
12 |
13 | // Concatenate two strings.
14 | println(str + " Hello, MoonBit!")
15 |
16 | // Use string interpolation.
17 | let moon = "Moon"
18 | let bit = "Bit"
19 | println("Use \{moon + bit}. Happy coding")
20 | }
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson6_string/index.md:
--------------------------------------------------------------------------------
1 | # 字符串
2 |
3 | 字符串是 UTF-16 编码的字符序列。在 MoonBit 中,字符串是不可变的,这意味着无法直接修改字符串中的字符。
4 |
5 | MoonBit 支持字符串和字符中的 C 风格转义字符,例如`\n`(换行符)、`\t`(制表符)、`\\`(反斜杠)、`\"`(双引号)和`\'`(单引号)。
6 |
7 | Moonbit 同时也支持 Unicode 转义字符,可使用`\u{...}`表示(其中`...`代表 Unicode 字符的十六进制码点)。
8 |
9 | MoonBit 还支持`\{变量}`形式的字符串插值语法,允许在字符串中嵌入表达式。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson7_tuple/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | // create Tuple
3 | let tuple = (3.14, false, [1,2,3])
4 | let tuple2 : (Float, Bool, Int) = (2.1, true, 20)
5 | println(tuple)
6 |
7 | // Accessing tuple elements
8 | println(tuple.0)
9 | println(tuple.2)
10 |
11 | // Tuple can also be destructured.
12 | let (a, b, c) = f()
13 | println("\{a}, \{b}, \{c}")
14 | }
15 |
16 | fn f() -> (Int, Bool, Double) {
17 | (1, false, 3.14) // return multiple values via tuple
18 | }
19 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson7_tuple/index.md:
--------------------------------------------------------------------------------
1 | # 元组
2 |
3 | 元组是包含不同类型值的不可变集合,使用圆括号创建。其特性包括:
4 |
5 | 元组是不可变的,即创建后无法修改其内容。可通过索引访问元素:`tuple.0`、`tuple.1`等。
6 |
7 | 元组可通过`let (a,b) = tuple`语法进行解构,其中右侧`tuple`必须是包含两个元素的元组,`a`和`b`将分别存储这两个元素。这是*模式匹配*的特例应用,将在后续章节详细讲解。
8 |
9 | 元组常用于函数返回多个值。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson8_map/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | // Create a map by map literal
3 | let map1 = { "key1": 1, "key2": 2, "key3": 3 }
4 | println(map1)
5 | // You can also create a map by Map::of, from a list of key-value pairs
6 | let map2 = Map::of([("key1", 1), ("key2", 2), ("key3", 3)])
7 | println(map1 == map2)
8 |
9 | // Access a value by key
10 | println(map1["key1"])
11 |
12 | // Update a value by key
13 | map1["key1"] = 10
14 | println(map1)
15 |
16 | // test a if a key exists
17 | println(map1.contains("key1"))
18 | }
19 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson8_map/index.md:
--------------------------------------------------------------------------------
1 | # 映射
2 |
3 | 映射是键值对的集合。每个键在映射中是唯一的,并且所有键都与一个值相关联。它是一个可变的集合。
4 |
5 | 形如 `{"key1": value1, "key2": value2}` 的表达式可以用来表示一个映射,称为 *映射字面量*。
6 | 如果映射的键和值类型是基本类型(`Int`、`String`、`Bool`、`Double` 等),则映射可以使用 *映射字面量*的语法来表示。
7 |
8 | 其他情况下,我们可以使用 `Map::of` 函数创建映射。它接受一个由两个元素的元组组成的数组,其中第一个元素是键,第二个元素是值。
9 |
10 | 可以使用 `map[key]` 语法通过键访问映射中的值。
11 |
12 | 可以使用以下语法更新映射中的元素:`map[key] = new_value`。
13 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson9_if_else/index.mbt:
--------------------------------------------------------------------------------
1 | fn fib(x : Int) -> Int {
2 | if x < 2 {
3 | x
4 | } else {
5 | fib(x - 1) + fib(x - 2)
6 | }
7 | }
8 |
9 | fn main {
10 | if 5 > 1 {
11 | println("5 is greater than 1")
12 | }
13 | println(fib(5))
14 | println(weekday(3))
15 | }
16 |
17 | fn weekday(x : Int) -> String {
18 | if x == 1 {
19 | "Monday"
20 | } else if x == 2 {
21 | "Tuesday"
22 | } else if x == 3 {
23 | "Wednesday"
24 | } else if x == 4 {
25 | "Thursday"
26 | } else if x == 5 {
27 | "Friday"
28 | } else if x == 6 {
29 | "Saturday"
30 | } else if x == 7 {
31 | "Sunday"
32 | } else {
33 | "Invalid day"
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter1_basics/lesson9_if_else/index.md:
--------------------------------------------------------------------------------
1 | # if 表达式
2 |
3 | if 表达式是带有返回值的条件控制流表达式。
4 |
5 | 在 if 表达式中:
6 | - 每个分支必须具有相同类型
7 | - 当条件为真时返回第一个分支的值
8 | - 否则返回第二个分支的值
9 |
10 | `else` 部分可省略。若省略,整个 if 表达式类型将为 `Unit`
11 |
12 | 在 else 部分中嵌套 if 表达式时,可简写为 `else if` 形式
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter2_data_types/lesson1_struct/index.mbt:
--------------------------------------------------------------------------------
1 | struct Point {
2 | x : Int
3 | y : Int
4 | } derive(Show)
5 |
6 | fn main {
7 | // create a point
8 | let point = { x: 3, y: 4 }
9 | println("point: \{point}")
10 | println("point.x: \{point.x}")
11 | println("point.y: \{point.y}")
12 |
13 | // functional update
14 | let point2 = {..point, x: 20}
15 | println(point2)
16 | }
17 |
18 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter2_data_types/lesson1_struct/index.md:
--------------------------------------------------------------------------------
1 | # 结构体
2 |
3 | 结构体是由其他类型组合而成的新类型。
4 |
5 | 在示例中,我们定义了一个包含两个整数字段`x`和`y`的结构体`Point`。
6 |
7 | 我们可以通过编写`{ x: 3, y: 4 }`的语法形式来创建`Point`实例。由于编译器可以通过字段标签`x`和`y`推断类型,因此可以省略结构体名称。
8 |
9 | 我们也可以显式添加`Point::`前缀来明确指定实例类型,这在需要消除类型歧义时非常有用。
10 |
11 | 与元组类似,我们可以使用`point.x`语法来访问结构体的字段。
12 |
13 | 结构体定义后的`derive(Show)`表示我们可以通过`println`函数直接打印结构体内容。
14 |
15 | 结构体字段默认是不可变的(immutable),创建后不可修改。Moonbit 提供称为*功能更新*(functional update)的语法,允许通过现有结构体创建包含更新字段的新结构体。
16 |
17 | 我们将在下一课中学习如何使结构体字段可变。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter2_data_types/lesson2_mutable_field/index.mbt:
--------------------------------------------------------------------------------
1 | struct MutPoint {
2 | mut mx : Int
3 | y : Int
4 | } derive(Show)
5 |
6 | fn main {
7 | let point = { mx: 3, y: 4 }
8 | println("point: \{point}")
9 | point.mx = 10
10 | println("point: \{point}")
11 | }
12 |
13 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter2_data_types/lesson2_mutable_field/index.md:
--------------------------------------------------------------------------------
1 | # 结构体的可变字段
2 |
3 | 结构体字段默认不可变,但可以通过在字段声明中使用`mut`关键字实现可变性。
4 |
5 | 在前面的课程中,我们已经了解 MoonBit 的集合类型可以通过类型声明中的`mut`关键字实现可变与不可变的控制。
6 |
7 | 示例中的`MutPoint`结构体包含两个字段:可变的`mx`和不可变的`y`。
8 | 您可以通过重新赋值修改`mx`字段的值,但无法修改`y`字段的值。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter2_data_types/lesson3_enum/index.md:
--------------------------------------------------------------------------------
1 | # 枚举类型
2 |
3 | 枚举类型通过列举可能值来定义新类型。与传统枚举不同,MoonBit 的枚举类型允许每个枚举项关联数据。我们将每个枚举项称为*枚举构造器*。
4 |
5 | 本示例中定义的`Color`枚举包含五个构造器:`Red`、`Green`、`Blue`、`RGB`和`CMYK`。其中`Red`、`Green`和`Blue`直接表示颜色值,而`RGB`和`CMYK`构造器则携带关联数据。
6 |
7 | `Red`和`RGB(255,255,255)`都属于`Color`类型的实例。如需显式创建实例,可采用类似结构体的`Color::Red`语法。
8 |
9 | 在`print_color`函数中,我们使用*模式匹配*来区分不同的枚举构造器。这种控制流类似于 C 系语言中的 switch-case 语句,不同之处在于:可以通过在`=>`左侧为关联数据命名,并在右侧作为变量使用这些数据。
10 |
11 | 我们将在后续章节深入探讨*模式匹配*的更强大功能。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter2_data_types/lesson4_newtype/index.mbt:
--------------------------------------------------------------------------------
1 | type UserId Int
2 | type UserName String
3 |
4 | fn main {
5 | let user_id : UserId = UserId(1)
6 | let user_name : UserName = UserName("Alice")
7 | println(user_id._)
8 | println(user_name._)
9 | // use some pattern matching to extract the values
10 | let UserId(id) = user_id
11 | let UserName(name) = user_name
12 | }
13 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter2_data_types/lesson4_newtype/index.md:
--------------------------------------------------------------------------------
1 | # 新类型模式(Newtype)
2 |
3 | 新类型模式(Newtype)类似于仅包含单个构造器的枚举类型(构造器名称与新类型模式(Newtype)本身相同)。您可以通过该构造器创建新类型值,并使用`._`操作符提取其内部表示。
4 |
5 | 新类型模式同样支持*模式匹配*的使用。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter2_data_types/lesson5_option/index.mbt:
--------------------------------------------------------------------------------
1 | fn first_char(s : String) -> Option[Char] {
2 | if s.length() == 0 {
3 | None
4 | } else {
5 | Some(s[0])
6 | }
7 | }
8 |
9 | fn main {
10 | let c1 : Char? = first_char("hello")
11 | let c2 : Option[Char] = first_char("")
12 | println("\{c1.is_empty()}, \{c1.unwrap()}")
13 | println("\{c2.is_empty()}, \{c2}")
14 | }
15 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter2_data_types/lesson5_option/index.md:
--------------------------------------------------------------------------------
1 | # Option 类型
2 |
3 | `Option[Char]`是表示可能存在的字符值的枚举类型,常用于处理可能缺失值的场景。其构造器包含两种可能:
4 |
5 | - `None` 表示值缺失
6 | - `Some(e)` 作为包装器包含实际值`e`
7 |
8 | 类型声明中的`[Char]`是类型参数,表示`Option`中值的类型为`Char`。同理可使用`Option[String]`、`Option[Double]`等类型。关于泛型的详细内容将在后续章节展开。
9 |
10 | 类型注解`Option[A]`可简写为`A?`。
11 |
12 | 您可以通过`c1.is_empty()`检查值是否缺失,使用`c1.unwrap()`获取包装值。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter2_data_types/lesson6_result/index.mbt:
--------------------------------------------------------------------------------
1 | fn first_char(s : String) -> Result[Char, String] {
2 | if s.length() == 0 {
3 | Err("empty string")
4 | } else {
5 | Ok(s[0])
6 | }
7 | }
8 |
9 | fn main {
10 | let c1 = first_char("hello")
11 | let c2 = first_char("")
12 | println("\{c1.is_ok()}, \{c1}, \{c1.unwrap()}")
13 | println("\{c2.is_err()}, \{c2}")
14 | }
15 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter2_data_types/lesson6_result/index.md:
--------------------------------------------------------------------------------
1 | # Result 类型
2 |
3 | 与`Option[Char]`类似,枚举类型`Result[Char, String]`表示可能存在的字符值。当值缺失时,可携带`String`类型的错误信息。
4 |
5 | 其构造器包含两种形式:
6 | - `Err("错误信息")` 表示值缺失并携带错误描述
7 | - `Ok('h')` 作为包装器包含实际值`'h'`
8 |
9 | 当前示例中`Option`和`Result`的处理方式较为冗长且易出错。为安全、简洁地处理这些类型,推荐使用:
10 | 1. *模式匹配* 进行安全处理
11 | 2. *错误处理* 机制有效管理错误
12 |
13 | 这两个主题将在后续章节详细讲解。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter3_pattern_matching/lesson1_introduction/index.mbt:
--------------------------------------------------------------------------------
1 | struct Point {
2 | x : Int
3 | y : Int
4 | } derive(Show)
5 |
6 | fn main {
7 | let tuple = (1, false, 3.14)
8 | let (a, b, c) = tuple
9 | println("a:\{a}, b:\{b}, c:\{c}")
10 | let record = { x: 5, y: 6 }
11 | let { x, y } = record
12 | println("x:\{x}, y:\{y}")
13 | }
14 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter3_pattern_matching/lesson1_introduction/index.md:
--------------------------------------------------------------------------------
1 | # 模式匹配
2 |
3 | 我们在之前的示例中已经演示过模式匹配的用法。
4 | 作为 MoonBit 的核心特性之一,模式匹配可应用于多种场景。它能帮助开发者高效实现条件判断,提升代码的精确性和健壮性。
5 |
6 | 本节将展示模式匹配的基础用法。该特性在其他语言中可能被称为"解构"或"结构化绑定",主要用于从复杂数据结构中提取值。
7 |
8 | 需要明确的是:"解构"只是模式匹配功能的子集。
9 | 在 MoonBit 中,几乎所有可构造的数据类型都支持对应的解构操作,这种解构形式我们称之为*模式*。
10 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter3_pattern_matching/lesson2_patterns_in_let/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | f([1, 2, 3])
3 | f([1, 2])
4 | }
5 |
6 | fn f(array : Array[Int]) -> Unit {
7 | let [a, b, c] = array // warning as error
8 | println("a:\{a}, b:\{b}, c:\{c}")
9 | }
10 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter3_pattern_matching/lesson2_patterns_in_let/index.md:
--------------------------------------------------------------------------------
1 | # `let` 语句中的模式
2 |
3 | 在 `let` 语句中,`=` 左侧可以是一个模式,用于匹配右侧的值。如果模式无法匹配,程序将中止。
4 |
5 | 您可能会注意到一个部分匹配警告错误。这是因为数组可能包含与预期不同数量的元素,而 `let` 语句仅处理数组恰好包含三个元素的情况。这被称为部分匹配。**部分匹配会使程序更加脆弱:模式匹配可能在其他情况下失败,从而导致程序中止。** 默认情况下,部分匹配警告被配置为错误,以帮助防止潜在的运行时问题。
6 |
7 | 实际上,`match` 表达式比 `let` 语句更常被使用。
8 |
9 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter3_pattern_matching/lesson3_patterns_in_match/index.md:
--------------------------------------------------------------------------------
1 | # `match` 表达式中的模式
2 |
3 | 在这个例子中,我们定义了一个描述文件系统的 `Resource` 类型。`Resource` 可以是一个文本文件、一个图像,或者是一个包含更多文件的文件夹。
4 |
5 | `count` 函数递归地遍历输入 `res`,并使用 `match` 表达式返回 `Image` 和 `TextFile` 的数量。
6 |
7 | `match` 表达式具有*第一个匹配语义*。它会从第一个分支到最后一个分支依次尝试找到第一个匹配的模式,并执行相应的匹配表达式。如果没有模式匹配,程序将中止。
8 |
9 | `match` 表达式的返回值是 `Int` 类型,因为所有分支的结果类型都是相同的 `Int`。
10 |
11 | 模式可以是嵌套的。如果你不关心枚举构造器中的数据,可以使用*任意模式*(写作 `_`)来代替引入新变量。
12 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter3_pattern_matching/lesson4_constant_pattern/index.md:
--------------------------------------------------------------------------------
1 | # 常量模式
2 |
3 | MoonBit 中几乎所有常量都可以作为常量模式使用。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter3_pattern_matching/lesson5_tuple_pattern/index.mbt:
--------------------------------------------------------------------------------
1 | fn logical_and(x : Bool, y : Bool) -> Bool {
2 | match (x, y) {
3 | (true, true) => true
4 | (false, _) => false
5 | (_, false) => false
6 | }
7 | }
8 |
9 | fn logical_or(x : Bool, y : Bool) -> Bool {
10 | match (x, y) {
11 | (true, _) => true
12 | (_, true) => true
13 | _ => false
14 | }
15 | }
16 |
17 | fn main {
18 | println("true and false: \{logical_and(true, false)}")
19 | println("true or false: \{logical_or(true, false)}")
20 | }
21 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter3_pattern_matching/lesson5_tuple_pattern/index.md:
--------------------------------------------------------------------------------
1 | # 元组模式
2 |
3 | 元组模式可用于同时匹配多个条件。
4 |
5 | 本示例通过模式匹配模拟*逻辑与*和*逻辑或*运算。
6 |
7 | 在此场景中,条件判断中创建元组的开销会被编译器优化消除。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter3_pattern_matching/lesson6_alias_pattern/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let (a, (b, _) as tuple, _) as triple = (1, (true, 5), false)
3 | println("a: \{a}, b: \{b}")
4 | println("tuple: \{tuple}")
5 | println("triple: \{triple}")
6 | }
7 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter3_pattern_matching/lesson6_alias_pattern/index.md:
--------------------------------------------------------------------------------
1 | # 别名模式
2 |
3 | 通过*别名模式*可将任意模式绑定到新名称,语法形式为`模式 as 名称`。本示例中,我们利用该特性在模式匹配过程的中可以保留原始元组结构。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter3_pattern_matching/lesson7_array_pattern/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | let array = [1, 2, 3, 4, 5, 6]
3 | let [a, b, ..] = array
4 | let [.., c, d] = array
5 | let [e, .., f] = array
6 | println("a: \{a}, b: \{b}")
7 | println("c: \{c}, d: \{d}")
8 | println("e: \{e}, f: \{f}")
9 | println("sum of array: \{sum(array[:])}")
10 | }
11 |
12 | fn sum(array : ArrayView[Int]) -> Int {
13 | match array {
14 | [] => 0
15 | [x, .. as xs] => x + sum(xs)
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter3_pattern_matching/lesson7_array_pattern/index.md:
--------------------------------------------------------------------------------
1 | # 数组模式
2 |
3 | 数组模式是一个用 `[]` 包围的模式序列,用于匹配数组。
4 |
5 | 你可以使用 `..` 来匹配数组的开始、结束或中间的其余部分。
6 |
7 | 在数组模式中,`..` 部分可以通过*别名模式*绑定到一个新变量。该变量的类型是 `ArrayView`。`sum` 函数使用此功能递归地计算数组的和。
8 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter3_pattern_matching/lesson8_or_pattern/index.mbt:
--------------------------------------------------------------------------------
1 | enum Color {
2 | Blue
3 | Red
4 | Green
5 | RGB(Int, Int, Int)
6 | RGBA(Int, Int, Int, Int)
7 | } derive(Show)
8 |
9 | fn get_green(color : Color) -> Int {
10 | match color {
11 | Blue | Red => 0
12 | Green => 255
13 | RGB(_, g, _) | RGBA(_, g, _, _) => g
14 | }
15 | }
16 |
17 | fn main {
18 | println("The green part of Red is \{get_green(Red)}")
19 | println("The green part of Green is \{get_green(Green)}")
20 | println("The green part of Blue is \{get_green(Blue)}")
21 | println("The green part of RGB(0,0,0) is \{get_green(RGB(0,0,0))}")
22 | println("The green part of RGBA(50,5,0,6) is \{get_green(RGBA(50,5,0,6))}")
23 | }
24 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter3_pattern_matching/lesson8_or_pattern/index.md:
--------------------------------------------------------------------------------
1 | # 或模式
2 |
3 | 当多个分支存在共享数据且处理逻辑相同时,单独处理会显得冗余。例如,本示例定义枚举类型`RGB`和用于提取绿色通道值的`get_green`函数。
4 |
5 | 通过*或模式*可将`RGB`和`RGBA`分支合并处理。在或模式中,子模式可引入新变量,但所有子模式的变量必须类型相同且命名一致,该限制确保后续能统一处理这些变量。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter3_pattern_matching/lesson9_range_pattern/index.md:
--------------------------------------------------------------------------------
1 | # 范围模式
2 |
3 | 当需要匹配连续值时,使用之前介绍的*或模式*会显得繁琐。为此,我们可以使用*范围模式*来匹配指定区间内的值。
4 |
5 | 回顾第一章学过的语法:
6 | - `start.. UInt {
2 | match list {
3 | Nil => 0
4 | Cons(_, rest) => count(rest) + 1 // may overflow
5 | }
6 | }
7 |
8 | ///|
9 | fn main {
10 | let empty_list : @immut/list.T[Int] = Nil
11 | let list_1 = @immut/list.Cons(1, empty_list)
12 | let list_2 = @immut/list.Cons(2, list_1)
13 | let list_3 = @immut/list.Cons(3, empty_list)
14 | let reversed_1 = list_1.rev()
15 | let n = count(reversed_1)
16 | }
17 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter4_more_data_types/lesson1_immutable_list/index.md:
--------------------------------------------------------------------------------
1 | # 不可变列表
2 |
3 | 不可变列表位于包 `@immut/list` 中。
4 |
5 | 它可以是:
6 |
7 | - `Nil` : 一个空列表
8 | - `Cons` : 一个元素和列表的其余部分。
9 |
10 | 因此,许多操作(例如反转列表)的复杂度为 O(n),并且可能会栈溢出。
11 |
12 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter4_more_data_types/lesson2_immutable_set/index.mbt:
--------------------------------------------------------------------------------
1 | ///|
2 | fn main {
3 | let a = @immut/hashset.of([5, 4, 3, 2, 1])
4 | let b = @immut/sorted_set.of([5, 4, 3, 2, 1])
5 | let arraya = a.iter().collect()
6 | let arrayb = b.iter().collect()
7 | let c = a.add(6)
8 | let d = b.add(6)
9 | let except_one = d.remove_min()
10 | println(d)
11 | }
12 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter4_more_data_types/lesson2_immutable_set/index.md:
--------------------------------------------------------------------------------
1 | # 不可变集合
2 |
3 | 不可变集合有两种实现:
4 |
5 | - `@immut/hashset` 中的哈希集合
6 | - `@immut/sorted_set` 中的排序集合
7 |
8 | 因此,它们的行为不同,API 也不同。
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter4_more_data_types/lesson3_immutable_map/index.mbt:
--------------------------------------------------------------------------------
1 | ///|
2 | fn main {
3 | let a = @immut/hashmap.of([(1, "a"), (2, "b"), (3, "c")])
4 | let b = a.add(4, "d")
5 | let c = @immut/sorted_map.from_iter(b.iter())
6 | println(c.keys())
7 | }
8 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter4_more_data_types/lesson3_immutable_map/index.md:
--------------------------------------------------------------------------------
1 | # 不可变映射
2 |
3 | 与不可变集合类似,有两种不可变映射:
4 |
5 | - `@immut/hashmap` 中的哈希映射
6 | - `@immut/sorted_map` 中的排序映射
7 |
8 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/chapter5_methods_and_traits/lesson1_methods/index.mbt:
--------------------------------------------------------------------------------
1 | type MyInt Int
2 |
3 | fn increment(self : MyInt) -> MyInt {
4 | MyInt(self._ + 1)
5 | }
6 |
7 | fn MyInt::println(x : MyInt) -> Unit {
8 | println("MyInt(\{x._})")
9 | }
10 |
11 | fn main {
12 | let x = MyInt(39)
13 | let y = x.increment() // 用 `.` 语法调用方法
14 | let z = increment(y) // `fn increment(..)` 可以直接调用
15 | let w = MyInt::increment(z) // 也可以用 `T::method_name` 调用
16 | w.println()
17 | MyInt::println(w) // `fn MyInt::println` 不能直接用 `println(..)` 调用
18 | }
19 |
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/index.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | println("hello")
3 | }
--------------------------------------------------------------------------------
/moonbit-tour/tour/zh/index.md:
--------------------------------------------------------------------------------
1 | # 欢迎来到 MoonBit 语言之旅!💫
2 |
3 | 本次教程涵盖了 MoonBit 语言的所有方面,需要您具备一定的编程基础。它将教您编写 MoonBit 实际程序所需的一切。
4 |
5 | 本次教程是互动的!显示的代码是可编辑的,并在您输入时实时编译和评估。使用 `println` 打印的任何内容都将显示在底部部分。为了评估 MoonBit 代码,本次教程将 MoonBit 编译为 wasm gc 并运行,整个过程全部在您的浏览器窗口内完成。
6 |
7 | 如果您遇到困难或有问题,欢迎随时在 MoonBit Discord 服务器上提问。我们非常乐意为您提供帮助,如果您对某些内容感到困惑,那么很可能其他人也会有同样的困惑,我们希望了解这些问题以改进本次教程。
8 |
9 | 好了,让我们开始吧。点击“下一步”开始,或点击“目录”跳转到特定主题。
10 |
11 |
--------------------------------------------------------------------------------
/next/.gitignore:
--------------------------------------------------------------------------------
1 | _build
2 | *.mo
3 | __pycache__
4 | .env
5 | download
--------------------------------------------------------------------------------
/next/Makefile:
--------------------------------------------------------------------------------
1 | # Minimal makefile for Sphinx documentation
2 | #
3 |
4 | # You can set these variables from the command line, and also
5 | # from the environment for the first two.
6 | SPHINXOPTS ?=
7 | SPHINXBUILD ?= sphinx-build
8 | SOURCEDIR = .
9 | BUILDDIR = _build
10 |
11 | # Put it first so that "make" without argument is like "make help".
12 | help:
13 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14 |
15 | .PHONY: help Makefile
16 |
17 | # Catch-all target: route all unknown targets to Sphinx using the new
18 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19 | %: Makefile
20 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
21 |
--------------------------------------------------------------------------------
/next/_ext/indent.py:
--------------------------------------------------------------------------------
1 | # https://github.com/executablebooks/MyST-Parser/issues/444
2 |
3 | # Workaround suggested in https://github.com/executablebooks/MyST-Parser/issues/444#issuecomment-1179796223
4 | from sphinx.transforms import i18n
5 |
6 | class ModifiedIndent:
7 | def __init__(self, s, _):
8 | self.s = s
9 | def __radd__(self, _):
10 | return f"```\n{self.s}\n```"
11 |
12 | i18n.indent = ModifiedIndent
13 |
14 | def setup(_app): pass
--------------------------------------------------------------------------------
/next/_static/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/_static/favicon.ico
--------------------------------------------------------------------------------
/next/_static/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/_static/logo.png
--------------------------------------------------------------------------------
/next/autobuild.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | set -e
3 |
4 | sphinx-autobuild . ./_build/html
--------------------------------------------------------------------------------
/next/example/index.md:
--------------------------------------------------------------------------------
1 | # Examples
2 |
3 | Here are some examples built with MoonBit.
4 |
5 | ```{only} html
6 | [Download this section in Markdown](path:/download/example/summary.md)
7 | ```
8 |
9 | ```{toctree}
10 | :maxdepth: 2
11 | :caption: Contents:
12 | sudoku/index
13 | lambda/index
14 | gmachine/index
15 | myers-diff/index
16 | segment-tree/index
--------------------------------------------------------------------------------
/next/example/myers-diff/index.md:
--------------------------------------------------------------------------------
1 | # Myers Diff
2 |
3 | ```{toctree}
4 | :maxdepth: 2
5 | :caption: Contents:
6 | myers-diff
7 | myers-diff2
8 | myers-diff3
--------------------------------------------------------------------------------
/next/imgs/add-deps.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/add-deps.png
--------------------------------------------------------------------------------
/next/imgs/coverage_html_index.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/coverage_html_index.png
--------------------------------------------------------------------------------
/next/imgs/coverage_html_page.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/coverage_html_page.png
--------------------------------------------------------------------------------
/next/imgs/haskell.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/haskell.png
--------------------------------------------------------------------------------
/next/imgs/import.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/import.png
--------------------------------------------------------------------------------
/next/imgs/lazy_evaluation.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/lazy_evaluation.png
--------------------------------------------------------------------------------
/next/imgs/moon-new.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/moon-new.png
--------------------------------------------------------------------------------
/next/imgs/moon-update.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/moon-update.png
--------------------------------------------------------------------------------
/next/imgs/reverse-array.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/reverse-array.png
--------------------------------------------------------------------------------
/next/imgs/runtime-installation.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/runtime-installation.png
--------------------------------------------------------------------------------
/next/imgs/segment-tree-add-lazytag.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/segment-tree-add-lazytag.png
--------------------------------------------------------------------------------
/next/imgs/segment-tree-add.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/segment-tree-add.png
--------------------------------------------------------------------------------
/next/imgs/segment-tree-build.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/segment-tree-build.png
--------------------------------------------------------------------------------
/next/imgs/segment-tree-lazytag.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/segment-tree-lazytag.png
--------------------------------------------------------------------------------
/next/imgs/segment-tree-query.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/segment-tree-query.png
--------------------------------------------------------------------------------
/next/imgs/smile_face_with_log.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/smile_face_with_log.png
--------------------------------------------------------------------------------
/next/imgs/sudoku.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/moonbitlang/moonbit-docs/84c582413f26cec4f3d8bcb25ad2c861962cc335/next/imgs/sudoku.jpg
--------------------------------------------------------------------------------
/next/language/error_codes/E0008.md:
--------------------------------------------------------------------------------
1 | # E0008
2 |
3 | The modifier (`pub`/`priv`) is redundant here, since this field has such
4 | visibility by default.
5 |
6 | ## Erroneous example
7 |
8 | ```moonbit
9 | struct A {
10 | priv value : Int
11 | // Warning: The private modifier is redundant here
12 | // since field value is private by default
13 | }
14 |
15 | pub struct B {
16 | pub value : Int
17 | // Warning: The public modifier is redundant here
18 | // since field value is public by default
19 | }
20 | ```
21 |
22 | ## Suggestion
23 |
24 | Remove the visibility modifier on the field.
25 |
26 | ```moonbit
27 | struct A {
28 | value : Int
29 | }
30 |
31 | pub struct B {
32 | value : Int
33 | }
34 | ```
35 |
--------------------------------------------------------------------------------
/next/language/error_codes/E0012.md:
--------------------------------------------------------------------------------
1 | # E0012
2 |
3 | Unreachable code. This usually happens when a `return` statement is followed by
4 | more code. The `return` statement will exit the function immediately, so any
5 | code after it will not be executed.
6 |
7 | ## Erroneous example
8 |
9 | ```moonbit
10 | fn main {
11 | return
12 | println("Hello, World!")
13 | }
14 | ```
15 |
16 | ## Suggestion
17 |
18 | Remove the unreachable code, or move it before the `return` statement if you
19 | want the code to be executed.
20 |
21 | ```moonbit
22 | fn main {
23 | println("Hello, World!")
24 | return
25 | }
26 | ```
27 |
--------------------------------------------------------------------------------
/next/language/error_codes/E0014.md:
--------------------------------------------------------------------------------
1 | # E0014
2 |
3 | Type name should be capitalized. In MoonBit, we recommend using a capitalized type name for all types, including built-in types and user-defined types. This is a convention that is followed by the standard library and most of the community code.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | struct point {
9 | x: Int
10 | y: Int
11 | }
12 | ```
13 |
14 | ## Suggestion
15 |
16 | Capitalize the type name.
17 |
18 | ```moonbit
19 | struct Point {
20 | x: Int
21 | y: Int
22 | }
23 | ```
24 |
--------------------------------------------------------------------------------
/next/language/error_codes/E0015.md:
--------------------------------------------------------------------------------
1 | # E0015
2 |
3 | The mutability is never used. It is a common mistake to declare an array to be
4 | mutable when it is not necessary. Setting the value of an element in an array
5 | does not require the array to be mutable. For example, `a[0] = 0` does not
6 | require `a` to be mutable, but `a = [0, 1, 2]` does.
7 |
8 | ## Erroneous example
9 |
10 | ```moonbit
11 | fn main {
12 | let mut x = [1, 2, 3] // Warning: The mutability of 'x' is never used.
13 | x[0] = 0
14 | println(x)
15 | }
16 | ```
17 |
18 | ## Suggestion
19 |
20 | Remove the `mut` keyword from the variable declaration.
21 |
22 | ```moonbit
23 | fn main {
24 | let x = [1, 2, 3]
25 | x[0] = 0
26 | println(x)
27 | }
28 | ```
29 |
--------------------------------------------------------------------------------
/next/language/error_codes/E0016.md:
--------------------------------------------------------------------------------
1 | # E0016
2 |
3 | Parser consistency check failed.
4 |
5 | Currently, MoonBit won't emit this warning.
6 |
--------------------------------------------------------------------------------
/next/language/error_codes/E0019.md:
--------------------------------------------------------------------------------
1 | # E0019
2 |
3 | Toplevel declaration is not left aligned. Every toplevel structure in MoonBit
4 | should be left-aligned, i.e. there is no space between the beginning of the line
5 | and the declaration.
6 |
7 | Sometimes, earlier parsing errors might causing this error to be raised in the
8 | following lines. Therefore, when your are writing local declarations while
9 | seeing this error, you should check the previous lines for any parsing errors.
10 |
11 | ## Erroneous example
12 |
13 | ```moonbit
14 | let x = 1
15 | ```
16 |
17 | ## Suggestion
18 |
19 | Remove the leading spaces:
20 |
21 | ```moonbit
22 | let x = 1
23 | ```
24 |
--------------------------------------------------------------------------------
/next/language/error_codes/E0023.md:
--------------------------------------------------------------------------------
1 | # E0023
2 |
3 | The body of this try expression never raises any error.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | fn main {
9 | try {
10 | println("Hello, world!")
11 | } catch {
12 | _ => println("Error")
13 | }
14 | }
15 | ```
16 |
17 | ## Suggestion
18 |
19 | Remove the `try` expression:
20 |
21 | ```moonbit
22 | fn main {
23 | println("Hello, world!")
24 | }
25 | ```
26 |
--------------------------------------------------------------------------------
/next/language/error_codes/E0024.md:
--------------------------------------------------------------------------------
1 | # E0024
2 |
3 | The error type of this function is never used.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | fn greet() -> Unit! {
9 | println("Hello, world!")
10 | }
11 | ```
12 |
13 | ## Suggestion
14 |
15 | Remove the error type:
16 |
17 | ```moonbit
18 | fn greet() -> Unit {
19 | println("Hello, world!")
20 | }
21 | ```
22 |
--------------------------------------------------------------------------------
/next/language/error_codes/E0028.md:
--------------------------------------------------------------------------------
1 | # E0028
2 |
3 | Unfinished code. In MoonBit, you can use `...` to mark a piece of code that is incomplete. You can still compile and run the code, but it will cause a runtime error when it is executed.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | fn f() -> Unit {
9 | ...
10 | }
11 | ```
12 |
13 | ## Suggestion
14 |
15 | Complete the code:
16 |
17 | ```moonbit
18 | fn f() -> Unit {
19 | println("Hello, world!")
20 | }
21 | ```
22 |
--------------------------------------------------------------------------------
/next/language/error_codes/E0030.md:
--------------------------------------------------------------------------------
1 | # E0030
2 |
3 | The package alias is empty. The default package alias will be used instead.
4 |
5 | ## Erroneous example
6 |
7 | ```json
8 | {
9 | "is-main": true,
10 | "import": [
11 | {
12 | "path": "username/hello/lib",
13 | "alias": "" // Warning: The package alias is empty. The default package alias will be used instead.
14 | }
15 | ]
16 | }
17 | ```
18 |
19 | ## Suggestion
20 |
21 | Use a non-empty alias for the package:
22 |
23 | ```json
24 | {
25 | "is-main": true,
26 | "import": [
27 | {
28 | "path": "username/hello/lib",
29 | "alias": "hello"
30 | }
31 | ]
32 | }
33 | ```
34 |
--------------------------------------------------------------------------------
/next/language/error_codes/E0034.md:
--------------------------------------------------------------------------------
1 | # E0034
2 |
3 | No longer emitted.
4 |
--------------------------------------------------------------------------------
/next/language/error_codes/E0037.md:
--------------------------------------------------------------------------------
1 | # E0037
2 |
3 | The loop label name is never used.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | fn read() -> BytesView {
9 | ...
10 | }
11 |
12 | fn main {
13 | let mut bytes = []
14 | read~: loop read() {
15 | [] => break
16 | bs => {
17 | bytes = [..bytes, ..bs]
18 | continue read()
19 | }
20 | }
21 | }
22 | ```
23 |
24 | ## Suggestion
25 |
26 | Remove the unused loop label.
27 |
28 | ```moonbit
29 | fn main {
30 | let mut bytes = []
31 | loop {
32 | let bs = read()
33 | if bs == [] {
34 | break
35 | }
36 | bytes = [..bytes, ..bs]
37 | }
38 | }
39 | ```
40 |
--------------------------------------------------------------------------------
/next/language/error_codes/E0039.md:
--------------------------------------------------------------------------------
1 | # E0039
2 |
3 | No longer emitted.
4 |
--------------------------------------------------------------------------------
/next/language/error_codes/E0043.md:
--------------------------------------------------------------------------------
1 | # E0043
2 |
3 | Unused attribute.
4 |
5 | An attribute is marked on an entity that does not support this attribute,
6 | so the attribute will not take effect.
7 |
8 | ## Erroneous example
9 | ```moonbit
10 | // the attribute is unused and has no effect
11 | #deprecated
12 | fn main {
13 | }
14 | ```
15 |
--------------------------------------------------------------------------------
/next/language/error_codes/E0044.md:
--------------------------------------------------------------------------------
1 | # E0044
2 |
3 | Inline wasm code is invalid.
4 |
5 | Either the inline wasm code has incorrect syntax, or it refers to unbound function.
6 |
7 | ## Erroneous example
8 | ```moonbit
9 | // unmatched parenthesis
10 | extern "wasm" fn f() =
11 | #| (func $f
12 |
13 | extern "wasm" fn g() =
14 | #| (func $g (call $does_not_exist))
15 | ```
16 |
17 | ## Suggestion
18 | Here are some pitfalls when writing inline wasm:
19 |
20 | - all variables and functions must start with `$`
21 |
--------------------------------------------------------------------------------
/next/language/error_codes/E0046.md:
--------------------------------------------------------------------------------
1 | # E0046
2 |
3 | Useless `..` in struct or constructor pattern.
4 |
5 | `..` can be used to ignore unmatched fields in struct pattern,
6 | or ignore unmatched labelled arguments in constructor pattern.
7 | But if all fields/labelled arguments are already matched, the `..` is useless,
8 | and this warning will be emitted.
9 |
10 | ## Erroneous example
11 | ```moonbit
12 | struct Point {
13 | x : Int
14 | y : Int
15 | }
16 |
17 | fn f(p : Point) -> Unit {
18 | let { x, y, .. } = p
19 | println(x + y)
20 | }
21 |
22 | test {
23 | f({ x: 1, y: 2 })
24 | }
25 | ```
26 |
--------------------------------------------------------------------------------
/next/language/error_codes/E1001.md:
--------------------------------------------------------------------------------
1 | # E1001
2 |
3 | There is an internal error occurred to the compiler. Usually this means you have
4 | discovered a bug in the compiler.
5 |
6 | A bug report containing the error description and relevant code would be
7 | greatly appreciated. You can submit the bug report here:
8 |
9 |
10 |
--------------------------------------------------------------------------------
/next/language/error_codes/E3002.md:
--------------------------------------------------------------------------------
1 | # E3002
2 |
3 | This source files contains errors in the syntax of the code.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | fn main() -> Unit {
9 | println("Hello, world!"
10 | }
11 | ```
12 |
13 | This example gives the following error on line 3:
14 |
15 | ```
16 | Parse error, unexpected token `}`, you may expect `,` or `)`.
17 | ```
18 |
19 | ... which indicates a missing closing parenthesis (`)`) in the `println` function call.
20 |
21 | ## Suggestion
22 |
23 | Change your code to strictly follow the MoonBit syntax rules.
24 |
25 | In the above example, the missing closing parenthesis should be added:
26 |
27 | ```moonbit
28 | fn main {
29 | println("Hello, world!")
30 | }
31 | ```
32 |
--------------------------------------------------------------------------------
/next/language/error_codes/E3003.md:
--------------------------------------------------------------------------------
1 | # E3003
2 |
3 | `init` and `main` function must have no arguments and no return value.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | fn main() -> Unit { // Error: Main function must have no arguments and no return value.
9 | println("Hello, world!")
10 | }
11 | ```
12 |
13 | ## Suggestion
14 |
15 | Remove the argument list and return type annotation, as:
16 |
17 | ```moonbit
18 | fn main {
19 | println("Hello, world!")
20 | }
21 | ```
22 |
--------------------------------------------------------------------------------
/next/language/error_codes/E3004.md:
--------------------------------------------------------------------------------
1 | # E3004
2 |
3 | Missing parameters list. Add `()` after the name of the function if it takes 0
4 | parameter.
5 |
6 | ## Erroneous example
7 |
8 | ```moonbit
9 | fn greet { // Error: Missing parameters list. Add `()` if function `greet` has 0 parameter.
10 | println("Hello, world!")
11 | }
12 | ```
13 |
14 | ## Suggestion
15 |
16 | Add `()` after the function name.
17 |
18 | ```moonbit
19 | fn greet() {
20 | println("Hello, world!")
21 | }
22 | ```
23 |
--------------------------------------------------------------------------------
/next/language/error_codes/E3005.md:
--------------------------------------------------------------------------------
1 | # E3005
2 |
3 | There is no such visibility for the entity (function/type/trait/...).
4 |
5 | Usually, this means that you put an `priv` visibility modifier on a entity is
6 | by-default private.
7 |
8 | See the [Access Control](/language/packages.md#access-control) section of for a
9 | detailed explanation on the visibility in MoonBit.
10 |
11 | ## Erroneous example
12 |
13 | ```moonbit
14 | priv let value = 3 // Error: No 'priv' visibility for value.
15 | ```
16 |
17 | ## Suggestion
18 |
19 | Remove the visibility modifier from the definition of the entity:
20 |
21 | ```moonbit
22 | let value = 3 // This is already `priv` by default.
23 | ```
24 |
--------------------------------------------------------------------------------
/next/language/error_codes/E3006.md:
--------------------------------------------------------------------------------
1 | # E3006
2 |
3 | There is no individual visibility control for enum constructors.
4 |
5 | Usually, this means that you put an `priv` or `pub` visibility modifier on a enum constructor.
6 |
7 | ## Erroneous example
8 |
9 | ```moonbit
10 | enum A {
11 | priv A1 // Error: No individual visibility for enum constructor.
12 | pub A2 // Error: No individual visibility for enum constructor.
13 | }
14 | ```
15 |
16 | ## Suggestion
17 |
18 | Remove the visibility modifier from the definition of the enum constructor:
19 |
20 | ```moonbit
21 | enum A {
22 | A1
23 | A2
24 | }
25 | ```
26 |
--------------------------------------------------------------------------------
/next/language/error_codes/E3007.md:
--------------------------------------------------------------------------------
1 | # E3007
2 |
3 | Wrong location of `..` in pattern match. Put `..` at the end of the pattern.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | struct S {
9 | a : Int
10 | b : Int
11 | c : Int
12 | }
13 |
14 | fn main {
15 | let s : S = { a : 1, b : 2, c : 3 }
16 | let { a, .., c } = s
17 | // ^^
18 | // Error: Unexpected `..` here, add `, ..` behind the last field to ignore the rest of struct.
19 | }
20 | ```
21 |
22 | ## Suggestion
23 |
24 | ```moonbit
25 | // ...
26 | fn main {
27 | // ...
28 | let { a, c, .. } = s
29 | }
30 | ```
31 |
--------------------------------------------------------------------------------
/next/language/error_codes/E3008.md:
--------------------------------------------------------------------------------
1 | # E3008
2 |
3 | There are multiple `..` patterns in array pattern. Remove until there is only one `..` pattern in array pattern.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | fn main {
9 | let array = [1, 2, 3, 4, 5]
10 | let [fst, .., .., snd] = array
11 | // ^^
12 | // Error: At most one `..` is allowed in array pattern.
13 | }
14 | ```
15 |
16 | ## Suggestion
17 |
18 | Remove the extra `..` pattern.
19 |
20 | ```moonbit
21 | fn main {
22 | // ...
23 | let [fst, .., snd] = array
24 | }
25 | ```
26 |
--------------------------------------------------------------------------------
/next/language/error_codes/E3011.md:
--------------------------------------------------------------------------------
1 | # E3011
2 |
3 | The assignment contains an invalid left-hand-side (LHS) expression,
4 | such as a constant or a constructor.
5 |
6 | ## Erroneous example
7 |
8 | ```moonbit
9 | const N = 4
10 |
11 | fn main {
12 | N = 5 // Error: Invalid left value for assignment.
13 | }
14 | ```
15 |
16 | ## Suggestion
17 |
18 | Change the LHS to a valid mutable memory location,
19 | such as a mutable variable or a mutable field:
20 |
21 | ```moonbit
22 | fn main {
23 | let mut n = 4
24 | n = 5
25 | }
26 | ```
27 |
--------------------------------------------------------------------------------
/next/language/error_codes/E3014.md:
--------------------------------------------------------------------------------
1 | # E3014
2 |
3 | Inline wasm syntax error. MoonBit will check if the inline wasm syntax is correct, including:
4 |
5 | - If the parenthesis are correctly matched.
6 | - If the wasm instructions are correctly formatted.
7 |
8 | ## Erroneous example
9 |
10 | ```moonbit
11 | extern "wasm" fn i32_load(addr : Int) -> Int = // Error: Inline wasm syntax error: unmatched parenthesis at 1:1-1:57
12 | #|(func (param i32) (result i32)
13 | #| (i32.load (local.get 0))
14 | ```
15 |
16 | ## Suggestion
17 |
18 | Fix the inline wasm syntax as suggested by the warning message.
19 |
20 | ```moonbit
21 | extern "wasm" fn i32_load(addr : Int) -> Int =
22 | #|(func (param i32) (result i32)
23 | #| (i32.load (local.get 0)))
24 | ```
25 |
--------------------------------------------------------------------------------
/next/language/error_codes/E3017.md:
--------------------------------------------------------------------------------
1 | # E3017
2 |
3 | JSON parse error. This will only appear in `moon.pkg.json` files only.
4 |
5 | It is very like that the editor you are using will have support for JSON
6 | installed (for example, Visual Studio Code has a built-in JSON parser). This
7 | error is likely to be caused by a missing comma or a missing closing brace.
8 |
9 | ## Erroneous example
10 |
11 | ```json
12 | {
13 | "is-main": true
14 | "import": [] // Error: Expect_comma_or_rbrace
15 | }
16 | ```
17 |
18 | ## Suggestion
19 |
20 | ```json
21 | {
22 | "is-main": true,
23 | "import": []
24 | }
25 | ```
26 |
--------------------------------------------------------------------------------
/next/language/error_codes/E3020.md:
--------------------------------------------------------------------------------
1 | # E3020
2 |
3 | Unexpected `=` in struct expression. The correct syntax for struct expression is
4 | `{ field: expression }`.
5 |
6 | ## Erroneous example
7 |
8 | ```moonbit
9 | struct S {
10 | a : Int
11 | b : Int
12 | }
13 |
14 | fn main {
15 | let s : S = { a : 1, b : 2 }
16 | let ss = { ..s, a = 1 } // Error: Unexpected `=` in struct expression.
17 | ignore(ss)
18 | }
19 | ```
20 |
21 | ## Suggestion
22 |
23 | Change the struct expression to use the correct syntax:
24 |
25 | ```moonbit
26 | struct S {
27 | a : Int
28 | b : Int
29 | }
30 |
31 | fn main {
32 | let s : S = { a : 1, b : 2 }
33 | let ss = { ..s, a : 1 }
34 | ignore(ss)
35 | }
36 | ```
37 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4000.md:
--------------------------------------------------------------------------------
1 | # E4000
2 |
3 | Generic type variable name is already used.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | struct Container[T, T] {
9 | value : T
10 | }
11 |
12 | fn transform[A, A](x : A) -> A {
13 | x
14 | }
15 | ```
16 |
17 | ## Suggestion
18 |
19 | Use different names for type variables:
20 |
21 | ```moonbit
22 | struct Container[T1, T2] {
23 | value : T1
24 | }
25 |
26 | fn transform[A, B](x : A) -> B {
27 | // ... implementation
28 | }
29 | ```
30 |
31 | Or remove the duplicate type parameter if you meant to use the same type:
32 |
33 | ```moonbit
34 | struct Container[T] {
35 | value : T
36 | }
37 |
38 | fn transform[A](x : A) -> A {
39 | x
40 | }
41 | ```
42 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4003.md:
--------------------------------------------------------------------------------
1 | # E4003
2 |
3 | This is a reserved type name. Cannot declare it as a type variable, type, or trait.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | struct Error {}
9 | ```
10 |
11 | `Error` is the reserved name for the built-in error type, so it cannot be used for a
12 | custom type. This will give the following error on line 1:
13 |
14 | ```
15 | "Error" is a reserved type name. Cannot declare it as type
16 | ```
17 |
18 | ## Suggestion
19 |
20 | Consider using another name for your type, such as `MyError` or `Error_`.
21 |
22 | ```moonbit
23 | struct MyError {}
24 | ```
25 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4004.md:
--------------------------------------------------------------------------------
1 | # E4004
2 |
3 | Trait methods cannot have type parameters (be polymorphic).
4 | MoonBit currently does not support generic/polymorphic methods within trait definitions.
5 |
6 | ## Erroneous Example
7 |
8 | ```moonbit
9 | trait Stringer {
10 | stringify[T: Show](Self, T) -> String
11 | }
12 | ```
13 |
14 | ## Suggestion
15 |
16 | Consider using dynamic dispatch instead of generics:
17 |
18 | ```moonbit
19 | trait Stringer {
20 | stringify(Self, &Show) -> String
21 | }
22 | ```
23 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4008.md:
--------------------------------------------------------------------------------
1 | # E4008
2 |
3 | FFI function cannot have type parameters.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | extern "js" fn id[T](x: T) -> T = "(x) => x"
9 | ```
10 |
11 | The example declares an FFI function (marked `extern`) with a type parameter,
12 | which is not allowed.
13 |
14 | ## Suggestion
15 |
16 | Consider using a concrete type that suits your needs:
17 |
18 | ```moonbit
19 | extern "js" fn int_id(x: Int) -> Int = "(x) => x"
20 | ```
21 |
22 | For more complicated scenarios, consider adding an extra trait:
23 |
24 | ```moonbit
25 | ///|
26 | trait Ider {
27 | id(Self) -> Self
28 | }
29 |
30 | ///|
31 | impl Ider for Int with id(self) { int_id(self) }
32 | ```
33 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4009.md:
--------------------------------------------------------------------------------
1 | # E4009
2 |
3 | Match function expects a different number of arguments than provided.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | let f = fn {
9 | 0 => 0
10 | a, b => a + b
11 | }
12 | ```
13 |
14 | The example defines a match function that has incoherent number of arguments in
15 | different branches. This will result in the following error on line 3:
16 |
17 | ```
18 | Match function expects 1 arguments, but 2 arguments are provided.
19 | ```
20 |
21 | ## Suggestion
22 |
23 | Change the branches so that the number of arguments are exactly the same in all
24 | of them:
25 |
26 | ```moonbit
27 | let f = fn {
28 | 0 => 0
29 | a => a
30 | }
31 | ```
32 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4011.md:
--------------------------------------------------------------------------------
1 | # E4011
2 |
3 | Type parameters are not allowed on default implementation for traits.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | pub(open) trait Stringer {
9 | stringify(Self) -> String
10 | }
11 |
12 | impl[T] Stringer with stringify(_self) { "hey" }
13 | ```
14 |
15 | The example above uses a type parameter `T` on the default implementation
16 | of the `Stringer` trait, which is not allowed.
17 |
18 | ## Suggestion
19 |
20 | Remove the type parameter from the default implementation:
21 |
22 | ```moonbit
23 | pub(open) trait Stringer {
24 | stringify(Self) -> String
25 | }
26 |
27 | impl Stringer with stringify(_self) { "hey" }
28 | ```
29 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4012.md:
--------------------------------------------------------------------------------
1 | # E4012
2 |
3 | Mutable constructor fields are only allowed on labelled arguments.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | enum E {
9 | F(mut x : Int)
10 | }
11 | ```
12 |
13 | The example above declares an unlabeled mutable field `x` in the
14 | constructor of the `F` variant of the `E` enum, which is not allowed.
15 |
16 | ## Suggestions
17 |
18 | Add a label to the mutable field:
19 |
20 | ```moonbit
21 | enum E {
22 | F(mut x~ : Int)
23 | }
24 | ```
25 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4014.md:
--------------------------------------------------------------------------------
1 | # E4014
2 |
3 | Type Mismatch.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | fn fst[X, Y] (a: X, b: Y) -> X {
9 | b
10 | }
11 | ```
12 |
13 | The above example returns a value `b` of type `Y` on line 2,
14 | which is not the same as the return type `X` of the function.
15 |
16 | ## Suggestion
17 |
18 | Please make sure to return a value of the same type as the return type
19 | of the function.
20 |
21 | For instance, in the example above, you may write:
22 |
23 | ```moonbit
24 | fn fst[X, Y] (a: X, b: Y) -> X {
25 | a
26 | }
27 | ```
28 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4015.md:
--------------------------------------------------------------------------------
1 | # E4015
2 |
3 | Type has no method with the specified name.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | let hey = "hey".upper()
9 | ```
10 |
11 | The above example tries to call an inexistent method `upper()`
12 | on a string literal, giving the following error on line 1:
13 |
14 | ```
15 | Type String has no method upper.
16 | ```
17 |
18 | ## Suggestion
19 |
20 | This is usually a typo or a misunderstanding of the methods available.
21 | Please make sure to use the correct method name:
22 |
23 | ```moonbit
24 | let hey = "hey".to_upper()
25 | ```
26 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4019.md:
--------------------------------------------------------------------------------
1 | # E4019
2 |
3 | The label is declared twice in this function.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | fn f(g~ : Int, g~ : String) -> Int {
9 | g
10 | }
11 | ```
12 |
13 | The above example declares the label `g` twice in the function `f`,
14 | which is not allowed and gives the following error on line 1:
15 |
16 | ```
17 | The label g~ is declared twice in this function, first in .mbt:1:6
18 | ```
19 |
20 | ## Suggestion
21 |
22 | Rename one of the labels to avoid the conflict:
23 |
24 | ```moonbit
25 | fn f(g~ : Int, h~ : String) -> Int {
26 | g
27 | }
28 | ```
29 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4021.md:
--------------------------------------------------------------------------------
1 | # E4021
2 |
3 | The value identifier is unbound.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | let a = @bool.to_integer(true)
9 | ```
10 |
11 | The example above tries to call the function `to_integer` in a package `@bool`,
12 | but this function is not present in the package `@bool`,
13 | giving the following error on line 1:
14 |
15 | ```
16 | Value to_integer not found in package `bool`.
17 | ```
18 |
19 | ## Suggestion
20 |
21 | Make sure the identifier is correct.
22 |
23 | In the above example, the function name should be `to_int` instead of `to_integer`:
24 |
25 | ```moonbit
26 | let a = @bool.to_int(true)
27 | ```
28 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4023.md:
--------------------------------------------------------------------------------
1 | # E4023
2 |
3 | The trait is not found.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | let a = true as &Sh0w
9 | ```
10 |
11 | The example above tries to cast a boolean value to an object
12 | of the `Sh0w` trait, but this trait is not found in the current scope,
13 | giving the following error on line 1:
14 |
15 | ```
16 | The trait Sh0w is not found.
17 | ```
18 |
19 | ## Suggestion
20 |
21 | Make sure the trait name is correct:
22 |
23 | ```moonbit
24 | let a = true as &Show
25 | ```
26 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4025.md:
--------------------------------------------------------------------------------
1 | # E4025
2 |
3 | No longer emitted.
4 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4026.md:
--------------------------------------------------------------------------------
1 | # E4026
2 |
3 | The field is not found.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | struct T { a : Int }
9 | fn main {
10 | let t = { b: 42 }
11 | }
12 | ```
13 |
14 | The example above tries to create a struct with a field `b`,
15 | but no struct with such a field is found in the current scope,
16 | giving the above error on line 3.
17 |
18 | ## Suggestion
19 |
20 | Make sure a struct is available in the current scope,
21 | defined with the correct field name:
22 |
23 | ```moonbit
24 | struct T { a : Int }
25 | fn main {
26 | let t = { a: 42 }
27 | }
28 | ```
29 |
30 | ... in the fix above, `t` is inferred to be of type `T`.
31 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4027.md:
--------------------------------------------------------------------------------
1 | # E4027
2 |
3 | Unused type parameter.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | fn id[T](a: Int) -> Int { a }
9 | ```
10 |
11 | The example above declares a type parameter `T` in the identity function `id`,
12 | however `T` is not used anywhere in the function signature,
13 | giving the following error on line 1:
14 |
15 | ```
16 | Unused type parameter 'T'
17 | ```
18 |
19 | ## Suggestion
20 |
21 | Make sure the type parameter is used in the function signature:
22 |
23 | ```moonbit
24 | fn id[T](a: T) -> T { a }
25 | ```
26 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4028.md:
--------------------------------------------------------------------------------
1 | # E4028
2 |
3 | This expression has a type which is not a struct.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | struct T { a : Int }
9 | let a : Int = { a: 42 }
10 | ```
11 |
12 | The example above tries to assign a struct of type `T` to a variable `a` of type `Int`,
13 | which is not possible and gives the following error on line 2:
14 |
15 | ```
16 | This expression has type Int, which is a Int type and not a struct.
17 | ```
18 |
19 | ## Suggestion
20 |
21 | Make sure to use the correct type instead:
22 |
23 | ```moonbit
24 | struct T { a : Int }
25 | let a : T = { a: 42 }
26 | ```
27 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4030.md:
--------------------------------------------------------------------------------
1 | # E4030
2 |
3 | The struct type does not have the specified field.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | struct T { a : Int }
9 | let t : T = { a: 42 }
10 | let u : T = { ..t, b: 43 }
11 | ```
12 |
13 | The example above tries to assign an updated struct with a field `b`
14 | to a variable `u` of type `T`, but this field doesn't exist,
15 | giving the following error on line 3:
16 |
17 | ```
18 | The struct type T does not have the field b.
19 | ```
20 |
21 | ## Suggestion
22 |
23 | Make sure to use the correct field instead:
24 |
25 | ```moonbit
26 | struct T { a : Int }
27 | let t : T = { a: 42 }
28 | let u : T = { ..t, a: 43 }
29 | ```
30 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4031.md:
--------------------------------------------------------------------------------
1 | # E4031
2 |
3 | The constructor is not found.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | enum U { V }
9 | let v : U = W
10 | ```
11 |
12 | The example above tries to assign a variant `W` to a variable `v` of type `U`,
13 | but this variant doesn't exist, giving the following error on line 2:
14 |
15 | ```
16 | The variant type U does not have the constructor W.
17 | ```
18 |
19 | ## Suggestion
20 |
21 | Make sure to use the correct constructor instead:
22 |
23 | ```moonbit
24 | enum U { V }
25 | let v : U = V
26 | ```
27 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4032.md:
--------------------------------------------------------------------------------
1 | # E4032
2 |
3 | The type is undefined.
4 |
5 | Currently, MoonBit won't emit this error.
6 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4033.md:
--------------------------------------------------------------------------------
1 | # E4033
2 |
3 | There is no struct definition with the specified fields.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | fn main {
9 | struct S { x : Int; y : Int }
10 | let c = { x: 2, w: 1 }
11 | }
12 | ```
13 |
14 | The example above tries to assign a struct with fields `x` and `w` to a variable `c`,
15 | but this field doesn't exist in any known struct type, giving the following error on line 3:
16 |
17 | ```
18 | There is no struct definition with the fields: x, w.
19 | ```
20 |
21 | ## Suggestion
22 |
23 | Make sure to use the correct field identifiers instead:
24 |
25 | ```moonbit
26 | fn main {
27 | struct S { x : Int; y : Int }
28 | let c = { x: 2, y: 1 }
29 | }
30 | ```
31 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4039.md:
--------------------------------------------------------------------------------
1 | # E4039
2 |
3 | There is no method with the specified name in this trait.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | let a : String = Show::to_str(42)
9 | ```
10 |
11 | The example above tries to call the method `to_str` on the `Show` trait,
12 | but the method is not defined in the trait, giving the following error on line 1:
13 |
14 | ```
15 | There is no method to_str in trait Show
16 | ```
17 |
18 | ## Suggestion
19 |
20 | Make sure that the method name is spelled correctly
21 | and that it is defined in the trait:
22 |
23 | ```moonbit
24 | let a : String = Show::to_string(42)
25 | ```
26 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4040.md:
--------------------------------------------------------------------------------
1 | # E4040
2 |
3 | The type constructor expects a different number of arguments than provided.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | let a : Option = Some(3)
9 | ```
10 |
11 | The example above tries to create an instance of the `Option` type.
12 | The `Option` type is a generic type that expects a single type argument,
13 | but the example does not provide any type arguments,
14 | giving the following error on line 1:
15 |
16 | ```
17 | The type constructor Option expects 1 argument(s), but is here given 0 argument(s).
18 | ```
19 |
20 | ## Suggestion
21 |
22 | Make sure to provide the correct number of type arguments:
23 |
24 | ```moonbit
25 | let a : Option[Int] = Some(3)
26 | ```
27 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4041.md:
--------------------------------------------------------------------------------
1 | # E4041
2 |
3 | Partial type annotation is not allowed in toplevel declaration.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | let a : Option[_] = Some(3)
9 | ```
10 |
11 | The example above tries to create a top-level instance of the `Option[_]` type.
12 | However, partial type annotation is not allowed in toplevel declaration,
13 | giving the aforementioned error on line 1.
14 |
15 | ## Suggestion
16 |
17 | Make sure to provide a complete type annotation:
18 |
19 | ```moonbit
20 | let a : Option[Int] = Some(3)
21 | ```
22 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4045.md:
--------------------------------------------------------------------------------
1 | # E4045
2 |
3 | The field is not defined in the struct type.
4 |
5 | ## Erroneous Example
6 |
7 | ```moonbit
8 | struct S { a : Int, b : Int }
9 | let a : Int = match S::{ a: 2, b: 3 } {
10 | { c: 2, .. } => 5
11 | _ => 6
12 | }
13 | ```
14 |
15 | The example above tries to match a struct with a nonexistent field `c`,
16 | giving the following error on line 3:
17 |
18 | ```
19 | The fields c is not defined in the struct type S.
20 | ```
21 |
22 | ## Suggestion
23 |
24 | Make sure to provide all fields with the correct names in the pattern.
25 |
26 | ```moonbit
27 | struct S { a : Int, b : Int }
28 | let a : Int = match S::{ a: 2, b: 3 } {
29 | { a: 2, .. } => 5
30 | _ => 6
31 | }
32 | ```
33 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4047.md:
--------------------------------------------------------------------------------
1 | # E4047
2 |
3 | Package not found when loading packages.
4 |
5 | Normally, this error should never be emitted. If you see this error, please
6 | report it as a bug at:
7 |
8 |
9 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4048.md:
--------------------------------------------------------------------------------
1 | # E4048
2 |
3 | The package file is in wrong format.
4 |
5 | This means your local build files are corrupted. Try to run `moon clean` or remove
6 | the `target` directory manually and build again. If the error persists, please
7 | submit a bug report here:
8 |
9 |
10 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4049.md:
--------------------------------------------------------------------------------
1 | # E4049
2 |
3 | Magic number mismatch for the package file.
4 |
5 | This means your local build files are corrupted or outdated. First try
6 | update/reinstall your MoonBit installation as in our [Download
7 | Page](https://www.moonbitlang.com/download/). Then try to run `moon clean`
8 | or remove the `target` directory manually and build again. If the error
9 | persists, please submit a bug report here:
10 |
11 |
12 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4052.md:
--------------------------------------------------------------------------------
1 | # E4052
2 |
3 | The type/trait name duplicates with a previously defined identifier.
4 |
5 | This can happen when you define a type and a trait with the same name.
6 |
7 | ## Erroneous example
8 |
9 | ```moonbit
10 | pub type A
11 | pub trait A {} // Error: The trait A duplicates with type A previously defined at ...
12 | ```
13 |
14 | ## Suggestion
15 |
16 | Rename either the type or the trait to a different name.
17 |
18 | ```moonbit
19 | pub type A
20 | pub trait B {}
21 | ```
22 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4054.md:
--------------------------------------------------------------------------------
1 | # E4054
2 |
3 | Cannot determine self type of extension method. [Self] does not occur in the
4 | signature of the method
5 |
6 | Currently, MoonBit won't emit this error.
7 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4055.md:
--------------------------------------------------------------------------------
1 | # E4055
2 |
3 | Field is already declared.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | struct A {
9 | a : Int
10 | a : Double
11 | }
12 | ```
13 |
14 | ## Suggestion
15 |
16 | Rename the field to a different name.
17 |
18 | ```moonbit
19 | struct A {
20 | a : Int
21 | b : Double
22 | }
23 | ```
24 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4057.md:
--------------------------------------------------------------------------------
1 | # E4057
2 |
3 | The constructor is duplicate.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | enum E {
9 | A
10 | A(Int)
11 | }
12 | ```
13 |
14 | ## Suggestion
15 |
16 | Rename the constructor to a different name.
17 |
18 | ```moonbit
19 | enum E {
20 | A
21 | B(Int)
22 | }
23 | ```
24 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4064.md:
--------------------------------------------------------------------------------
1 | # E4064
2 |
3 | Invalid escape sequence in char/string literal.
4 |
5 | ## Erroneous example
6 |
7 | Using escape sequence with invalid unicode code point result in this error:
8 |
9 | ```moonbit
10 | test {
11 | println('\uD800')
12 | }
13 | ```
14 |
15 | Another case is using unsupported escape sequence in char literal for `Byte`:
16 |
17 | ```moonbit
18 | test {
19 | let _ : Byte = '\uD000'
20 | }
21 | ```
22 |
23 | Only `\x` and `\o` escape sequences are supported in `Byte` literal.
24 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4072.md:
--------------------------------------------------------------------------------
1 | # E4072
2 |
3 | Method of trait already has a default implementation.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | trait T {
9 | to_int(Self) -> Int
10 | }
11 |
12 | type A Int
13 |
14 | impl T with to_int(self : Self) -> Int {
15 | 0
16 | }
17 |
18 | impl T with to_int(self : Self) -> Int { // Error: Method to_int of trait T already has a default implementation at
19 | 0
20 | }
21 | ```
22 |
23 | ## Suggestion
24 |
25 | Remove the duplicated default implementation of the trait.
26 |
27 | ```moonbit
28 | impl T with to_int(self : Self) -> Int {
29 | 0
30 | }
31 |
32 | // Remove this implementation
33 | // impl T with to_int(self : Self) -> Int {
34 | // 0
35 | // }
36 | ```
37 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4075.md:
--------------------------------------------------------------------------------
1 | # E4075
2 |
3 | Missing type annotation for the parameter.
4 |
5 | MoonBit requires all toplevel function to have full type annotation.
6 |
7 | ## Erroneous example
8 |
9 | ```moonbit
10 | fn f(param) -> Unit { // Error: Missing type annotation for the parameter param.
11 | }
12 | ```
13 |
14 | ## Suggestion
15 |
16 | Add type annotation for the parameter:
17 |
18 | ```moonbit
19 | fn f(param: Int) -> Unit {
20 | }
21 | ```
22 |
23 | Or if the parameter should be generic:
24 |
25 | ```moonbit
26 | fn f[T](param: T) -> Unit {
27 | }
28 | ```
29 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4076.md:
--------------------------------------------------------------------------------
1 | # E4076
2 |
3 | Missing type annotation for the return value.
4 |
5 | MoonBit requires all toplevel function to have full type annotation. It is a
6 | common mistake for people to forget to include the return type of a function,
7 | especially when the function returns `Unit`.
8 |
9 | ## Erroneous example
10 |
11 | ```moonbit
12 | fn f() { // Error: Missing type annotation for the return value.
13 | }
14 | ```
15 |
16 | ## Suggestion
17 |
18 | Add the return type of the function:
19 |
20 | ```moonbit
21 | fn f() -> Unit {
22 | }
23 | ```
24 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4077.md:
--------------------------------------------------------------------------------
1 | # E4077
2 |
3 | Don't know how to derive trait for type.
4 |
5 | MoonBit allows you to derive implementation of some pre-defined traits for your
6 | types. It means that you cannot have MoonBit automatically derive a trait that
7 | you defined yourself.
8 |
9 | ## Erroneous example
10 |
11 | ```moonbit
12 | trait T {
13 | f(Self) -> Int
14 | }
15 |
16 | type A Int derive(T) // Error: Don't know how to derive trait T for type A
17 | ```
18 |
19 | ## Suggestion
20 |
21 | You can implement the trait manually:
22 |
23 | ```moonbit
24 | trait T {
25 | f(Self) -> Int
26 | }
27 |
28 | // Remove derive(T)
29 | type A Int
30 |
31 | impl T for A with f(self : A) -> Int {
32 | 0
33 | }
34 | ```
35 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4078.md:
--------------------------------------------------------------------------------
1 | # E4078
2 |
3 | Cannot derive trait for type.
4 |
5 | MoonBit allows you to derive implementation of some traits for your types.
6 | However, not all types can be automatically derived. For example, you cannot
7 | derive a trait for an abstract type.
8 |
9 | ## Erroneous example
10 |
11 | ```moonbit
12 | type T derive(Hash) // Error: Cannot derive trait Hash for type T: target type is abstract
13 | ```
14 |
15 | ## Suggestion
16 |
17 | You can implement the trait manually:
18 |
19 | ```moonbit
20 | type T
21 |
22 | impl Hash for T with hash_combine(self : T, hasher: Hasher) {
23 | // ...
24 | }
25 | ```
26 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4082.md:
--------------------------------------------------------------------------------
1 | # E4082
2 |
3 | Variable is not bound in all patterns.
4 |
5 | When using the `|` operator in a pattern, all variables must be bound in all
6 | patterns. If a variable were not bound in all patterns, it would be free
7 | variable when the pattern is matched, which is not allowed.
8 |
9 | ## Erroneous Example
10 |
11 | ```moonbit
12 | enum E {
13 | A(Int, Double)
14 | B(Int)
15 | }
16 |
17 | fn f(value : E) -> Unit {
18 | match value {
19 | A(a, _) | B(_) => println("Hello") // Error: Variable a is not bound in all patterns.
20 | }
21 | }
22 | ```
23 |
24 | ## Suggestion
25 |
26 | ```moonbit
27 | fn f(value : E) -> Unit {
28 | match value {
29 | A(a, _) | B(a) => println("Hello")
30 | }
31 | }
32 | ```
33 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4083.md:
--------------------------------------------------------------------------------
1 | # E4083
2 |
3 | The type does not implement `op_as_view` method.
4 |
5 | This error code shall not be emitted when you are not developing the core
6 | library. If you see this error, please report it as a bug to the core library
7 | here:
8 |
9 |
10 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4090.md:
--------------------------------------------------------------------------------
1 | # E4090
2 |
3 | Tuples are not mutable. You cannot change the value of a field in a tuple using
4 | assignment.
5 |
6 | ## Erroneous example
7 |
8 | ```moonbit
9 | fn main {
10 | let a = (1, 2, 3)
11 | a.2 = 4 // Error: tuples are not mutable
12 | println(a.2)
13 | }
14 | ```
15 |
16 | ## Suggestion
17 |
18 | If you need to change the value of a field in a tuple, you should use a struct
19 | instead.
20 |
21 | ```moonbit
22 | pub struct MyStruct {
23 | a: Int
24 | b: Int
25 | mut c: Int
26 | }
27 |
28 | fn main {
29 | let a : MyStruct = { a : 1, b : 2, c : 3 }
30 | a.c = 4
31 | println(a.c)
32 | }
33 | ```
34 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4092.md:
--------------------------------------------------------------------------------
1 | # E4092
2 |
3 | Missing annotation for this empty struct.
4 |
5 | Currently, the MoonBit compiler does not emit this error, as it is impossible to
6 | create an empty struct without using `T::{ .. }` syntax.
7 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4093.md:
--------------------------------------------------------------------------------
1 | # E4093
2 |
3 | The type is not a struct type.
4 |
5 | This error occurs when you try to construct a type that is not a `struct` using
6 | the `T::{ .. }` syntax.
7 |
8 | ## Erroneous example
9 |
10 | ```moonbit
11 | enum Point {
12 | D2(Double, Double)
13 | D3(Double, Double, Double)
14 | }
15 |
16 | fn main {
17 | let a = Point::{ x : 1.0, y : 2.0 }
18 | // ^~~~~
19 | // Error: The type Point is not a struct type
20 | }
21 | ```
22 |
23 | ## Suggestion
24 |
25 | You should use the correct syntax to construct the type.
26 |
27 | ```moonbit
28 | fn main {
29 | let a = Point::D2(1.0, 2.0)
30 | }
31 | ```
32 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4096.md:
--------------------------------------------------------------------------------
1 | # E4096
2 |
3 | String interpolation syntax is not supported for `Bytes`.
4 | Only `String` supports string interpolation.
5 |
6 | ## Erroneous example
7 | ```moonbit
8 | test {
9 | let x : Int = 42
10 | let _ : Bytes = "(\{x})"
11 | }
12 | ```
13 |
14 | ## Suggestion
15 | To dynamically create a `Bytes`, use `@buffer.T` instead:
16 |
17 | ```moonbit
18 | test {
19 | let x : Int = 42
20 | let buf = @buffer.new()
21 | let _ : Bytes = buf
22 | ..write_string("(")
23 | ..write_object(x)
24 | ..write_string(")")
25 | .to_bytes()
26 | }
27 | ```
28 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4099.md:
--------------------------------------------------------------------------------
1 | # E4099
2 |
3 | A trait is used in context where a type is needed.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | // `Show` is a trait, not a type
9 | fn f(_ : Show) -> Unit {
10 |
11 | }
12 | ```
13 |
14 | ## Suggestion
15 | If you want to refer to a trait object type, use the syntax `&Trait` instead.
16 |
17 | ```moonbit
18 | fn f(_ : &Show) -> Unit {
19 |
20 | }
21 | ```
22 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4111.md:
--------------------------------------------------------------------------------
1 | # E4111
2 |
3 | The usage of break statement is invalid.
4 |
5 | This error happens when you use a `break` statement in the initialization,
6 | condition, or update statement of a loop.
7 |
8 | ## Erroneous example
9 |
10 | ```moonbit
11 | pub fn f(x: Int, y: Int) -> Unit {
12 | for i = 0; i < x; i = i + 1 {
13 | for j = { break }; j < y; j = j + 1 {
14 | // ^^^^^ Error: The usage of break statement is invalid.
15 | println(i + j)
16 | }
17 | }
18 | }
19 | ```
20 |
21 | ## Suggestion
22 |
23 | Do not write `break` statement in the initialization, condition, or update
24 | statement of a loop.
25 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4112.md:
--------------------------------------------------------------------------------
1 | # E4112
2 |
3 | The usage of continue statement is invalid.
4 |
5 | This error happens when you use a `continue` statement in the initialization,
6 | condition, or update statement of a loop.
7 |
8 | ## Erroneous example
9 |
10 | ```moonbit
11 | pub fn f(x: Int, y: Int) -> Unit {
12 | for i = 0; i < x; i = i + 1 {
13 | for j = { continue }; j < y; j = j + 1 {
14 | // ^^^^^^^^ Error: The usage of continue statement is invalid.
15 | println(i + j)
16 | }
17 | }
18 | }
19 | ```
20 |
21 | ## Suggestion
22 |
23 | Do not write `continue` statement in the initialization, condition, or update
24 | statement of a loop.
25 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4115.md:
--------------------------------------------------------------------------------
1 | # E4115
2 |
3 | Cannot auto-fill parameter of this type.
4 |
5 | MoonBit supports and auto-filling `SourceLoc` and `ArgsLoc` parameters in
6 | functions. Leaving the default value of parameter of other types will result in
7 | this error.
8 |
9 | ## Erroneous example
10 |
11 | ```moonbit
12 | fn f(parameter~ : Int = _) -> Unit {
13 | // ^~~~~~~~~ Error: Cannot auto-fill parameter of type Int
14 | }
15 | ```
16 |
17 | ## Suggestion
18 |
19 | Use a default value for the parameter:
20 |
21 | ```moonbit
22 | fn f(parameter~ : Int = 0) -> Unit {
23 | }
24 | ```
25 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4116.md:
--------------------------------------------------------------------------------
1 | # E4116
2 |
3 | Found hole _.
4 |
5 | If you want to represent unfinished code, we recommend using `...` instead.
6 | Using `...` allows the program to be compiled and run, while `_` will cause a
7 | compilation error.
8 |
9 | ## Erroneous example
10 |
11 | ```moonbit
12 | fn main {
13 | _ // Error: Found hole _
14 | }
15 | ```
16 |
17 | ## Suggestion
18 |
19 | Use `...` instead:
20 |
21 | ```moonbit
22 | fn main {
23 | ... // Warning: unfinished code
24 | }
25 | ```
26 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4124.md:
--------------------------------------------------------------------------------
1 | # E4124
2 |
3 | The constructor is ambiguous: it may come from multiple types.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | enum A {
9 | A(Int)
10 | B(Bool)
11 | C(Double)
12 | }
13 |
14 | enum B {
15 | A(Double)
16 | B(Int)
17 | C(Bool)
18 | }
19 |
20 | fn main {
21 | let a = A(1) // Error: The constructor A is ambiguous: it may come from type B or A.
22 | }
23 | ```
24 |
25 | ## Suggestion
26 |
27 | Add `T::` before the constructor:
28 |
29 | ```moonbit
30 | fn main {
31 | let a = A::A(1)
32 | }
33 | ```
34 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4127.md:
--------------------------------------------------------------------------------
1 | # E4127
2 |
3 | Type is not an error type.
4 |
5 | MoonBit only allow error types (defined with `type!` keyword) to appear after
6 | the exclamation mark (`!`) in the return type of a function.
7 |
8 | ## Erroneous example
9 |
10 | ```moonbit
11 | pub fn may_raise_error() -> Unit!String {
12 | // ^~~~~~
13 | // Error: Type String is not an error type.
14 | raise "Failed" // Error: Type String is not an error type.
15 | }
16 | ```
17 |
18 | ## Suggestion
19 |
20 | You can wrap the type you wish to raise in a error type:
21 |
22 | ```moonbit
23 | type! StringError String
24 |
25 | pub fn may_raise_error() -> Unit!StringError {
26 | raise StringError("Failed")
27 | }
28 | ```
29 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4128.md:
--------------------------------------------------------------------------------
1 | # E4128
2 |
3 | Target of type alias must not be a type parameter.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | typealias Wrap[T] = T // Error: Target of type alias must not be a type parameter.
9 | ```
10 |
11 | ## Suggestion
12 |
13 | Use the type directly without using a typealias:
14 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4129.md:
--------------------------------------------------------------------------------
1 | # E4129
2 |
3 | Found cycle in type alias.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | typealias A = B // Found cycle A -> B -> A in type alias.
9 | typealias B = A
10 | ```
11 |
12 | ## Suggestion
13 |
14 | You can break cycle using new type:
15 |
16 | ```moonbit
17 | type A B
18 | typealias B = A
19 | ```
20 |
21 | However, this is equivalent to writing
22 |
23 | ```moonbit
24 | type A A
25 | ```
26 |
27 | And this might not be want to want to do. Therefore, we recommend using
28 | newtypes instead of typealiases in most of times:
29 |
30 | ```moonbit
31 | type A B
32 | type B A
33 | ```
34 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4132.md:
--------------------------------------------------------------------------------
1 | # E4132
2 |
3 | Invalid test parameter. Only one parameter with type `@test.T` or `@bench.T` is allowed.
4 |
5 | ## Erroneous example
6 |
7 | ```moonbit
8 | test (name : String) {
9 | ...
10 | }
11 | ```
12 |
13 | ## Suggestion
14 |
15 | It is not yet possible to pass custom parameters to tests, and you have to use
16 | the predefined `@test.T` type or `@bench.T` type.
17 |
18 | ```moonbit
19 | test (it : @test.T) {
20 | ...
21 | }
22 |
23 | test (bm : @bench.T) {
24 | ...
25 | }
26 | ```
27 |
28 | Or you can remove the parameter if it is not needed:
29 |
30 | ```moonbit
31 | test {
32 | ...
33 | }
34 | ```
35 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4137.md:
--------------------------------------------------------------------------------
1 | # E4137
2 |
3 | Range operators are currently only supported in `for .. in` loops.
4 |
5 | This means you cannot save the range operator in a variable or use it later in
6 | other contexts.
7 |
8 | ## Erroneous example
9 |
10 | ```moonbit
11 | fn main {
12 | let range = 0..<10 // Error: Range operators are currently only supported in `for .. in` loops.
13 | }
14 | ```
15 |
16 | ## Suggestion
17 |
18 | One can use the `until` method to generate a range of numbers:
19 |
20 | ```moonbit
21 | fn main {
22 | let range = (0).until(10)
23 | for i in range {
24 | println(i)
25 | }
26 | }
27 | ```
28 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4139.md:
--------------------------------------------------------------------------------
1 | # E4139
2 |
3 | This expression has type that cannot be implicitly ignored. Use `ignore(...)` or
4 | `let _ = ...` to explicitly ignore it.
5 |
6 | `Unit` type can be implicitly ignored.
7 |
8 | ## Erroneous example
9 |
10 | For example,
11 |
12 | ```moonbit
13 | fn main {
14 | 1 + 1 // This expression has type Int, its value cannot be implicitly ignored.
15 | }
16 | ```
17 |
18 | The code here show a deeper problem of the logic of the code: discarding the
19 | result makes the computation useless.
20 |
21 | ## Suggestion
22 |
23 | If you do want to discard the result, use `ignore(..)` or `let _ =` to
24 | explicitly discard the value.
25 |
26 | ```moonbit
27 | fn main {
28 | ignore(1 + 1)
29 | }
30 | ```
31 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4140.md:
--------------------------------------------------------------------------------
1 | # E4140
2 |
3 | Invalid C function name in extern "C" declaration.
4 |
5 | When binding a C function with the `extern "C"` declaration, the function name
6 | must be a valid C identifier. This means that the name must satisfy the
7 | following regex:
8 |
9 | ```
10 | [a-zA-Z_$][a-zA-Z0-9_$]*
11 | ```
12 |
13 | ## Erroneous example
14 |
15 | ```moonbit
16 | extern "C" fn f1() = "1" // Error: Invalid C function name in extern "C" declaration
17 | ```
18 |
19 | ## Suggestion
20 |
21 | Change the function name to a valid C identifier:
22 |
23 | ```moonbit
24 | extern "C" fn f1() = "f1"
25 | ```
26 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4149.md:
--------------------------------------------------------------------------------
1 | # E4149
2 |
3 | Cannot call async function in this context.
4 |
5 | Async functions can only be called in async functions.
6 |
7 | ## Erroneous example
8 |
9 | ```moonbit
10 | async fn f() -> Int {
11 | ...
12 | }
13 |
14 | fn g() -> Int {
15 | f!()
16 | }
17 | ```
18 |
19 | ## Suggestion
20 |
21 | You can change the containing function to an async function:
22 |
23 | ```moonbit
24 | async fn g() -> Int {
25 | f!()
26 | }
27 | ```
28 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4150.md:
--------------------------------------------------------------------------------
1 | # E4150
2 |
3 | Async function call must be marked with `!`.
4 |
5 | MoonBit require all async function calls to be marked with `!`, as a way to to
6 | inform the programmer that the function call is async and the control flow will
7 | be suspended here.
8 |
9 | ## Erroneous example
10 |
11 | ```moonbit
12 | async fn f() -> Int {
13 | ...
14 | }
15 |
16 | async fn g() -> Int {
17 | f() // Error: async function call must be marked with `!`
18 | }
19 | ```
20 |
21 | ## Suggestion
22 |
23 | Mark the async function call with `!`:
24 |
25 | ```moonbit
26 | async fn g() -> Int {
27 | f!()
28 | }
29 | ```
30 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4153.md:
--------------------------------------------------------------------------------
1 | # E4153
2 |
3 | Duplicated tag in `enum` with custom tag value.
4 |
5 | For constant `enum`, where all constructors have no payload,
6 | MoonBit supports customizing the integer representation of constructors:
7 |
8 | ```moonbit
9 | enum Flag {
10 | A = 1
11 | B = 2
12 | C = 3
13 | }
14 | ```
15 |
16 | However, the tag of each constructor must be unique, otherwise this error will be reported.
17 |
18 | ## Erroneous example
19 | ```moonbit
20 | enum Bad {
21 | A = 1
22 | B // the tag of `B` is `A + 1 = 2`
23 | C = 2 // duplicates with `B`
24 | }
25 | ```
26 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4154.md:
--------------------------------------------------------------------------------
1 | # E4154
2 |
3 | Non-constant `enum` with custom tag value.
4 |
5 | MoonBit supports customizing the integer representation of constructors:
6 |
7 | ```moonbit
8 | enum Flag {
9 | A = 1
10 | B = 2
11 | C = 3
12 | }
13 | ```
14 |
15 | However, only constant `enum` (all constructors have not payload) can have custom tag.
16 | Otherwise this error will be reported.
17 |
18 | ## Erroneous example
19 | ```moonbit
20 | enum Bad {
21 | A = 1
22 | B = 2
23 | C(Int)
24 | }
25 | ```
26 |
--------------------------------------------------------------------------------
/next/language/error_codes/E4155.md:
--------------------------------------------------------------------------------
1 | # E4155
2 |
3 | Cycle in `const` or `fnalias` declaration.
4 |
5 | `const` declaration can refer to other constants defined in the same package,
6 | and `fnalias` can alias other function alias in the same package, too.
7 | But these definitions must not form cycle, otherwise this error will be reported.
8 |
9 | ## Erroneous example
10 | ```moonbit
11 | const A : Int = B + 1
12 | const B : Int = A + 1
13 |
14 | fnalias f = g
15 | fnalias g = f
16 | ```
17 |
--------------------------------------------------------------------------------
/next/language/error_codes/index.md:
--------------------------------------------------------------------------------
1 | # Error Codes Index
2 |
3 | ```{warning}
4 | The error codes index is currently WIP.
5 |
6 | Many of the entries contains only verify brief description of the error code.
7 | You are more than welcomed to expand any of the entries by submitting a PR to
8 | [moonbitlang/moonbit-docs](https://github.com/moonbitlang/moonbit-docs).
9 | ```
10 |
11 | This page lists all error codes produced by the MoonBit compiler.
12 |
13 | ```{toctree}
14 | :titlesonly:
15 | :glob:
16 | E*
17 | ```
18 |
--------------------------------------------------------------------------------
/next/sources/async/.gitignore:
--------------------------------------------------------------------------------
1 | target
2 |
--------------------------------------------------------------------------------
/next/sources/async/moon.mod.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "moonbit-community/async-doc",
3 | "version": "0.1.0",
4 | "readme": "README.md",
5 | "repository": "",
6 | "license": "Apache-2.0",
7 | "keywords": [],
8 | "description": "",
9 | "source": "src"
10 | }
11 |
--------------------------------------------------------------------------------
/next/sources/async/src/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-1-2-4-13-28",
3 | "targets": {
4 | "async.mbt": [
5 | "js"
6 | ]
7 | },
8 | "supported-targets": [
9 | "js"
10 | ]
11 | }
--------------------------------------------------------------------------------
/next/sources/diff/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | .mooncakes/
3 |
--------------------------------------------------------------------------------
/next/sources/diff/README.md:
--------------------------------------------------------------------------------
1 | # moonbit-community/diff
--------------------------------------------------------------------------------
/next/sources/diff/moon.mod.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "moonbit-community/diff",
3 | "version": "0.1.0",
4 | "readme": "README.md",
5 | "repository": "",
6 | "license": "Apache-2.0",
7 | "keywords": [],
8 | "description": "",
9 | "source": "src"
10 | }
--------------------------------------------------------------------------------
/next/sources/diff/src/part1/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-4"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/diff/src/part2/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-4"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/diff/src/part3/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-4"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/gmachine/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | .mooncakes/
3 |
--------------------------------------------------------------------------------
/next/sources/gmachine/README.md:
--------------------------------------------------------------------------------
1 | # G-Machine
--------------------------------------------------------------------------------
/next/sources/gmachine/moon.mod.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "moonbit-community/gmachine",
3 | "version": "0.1.0",
4 | "readme": "README.md",
5 | "repository": "",
6 | "license": "Apache-2.0",
7 | "keywords": [],
8 | "description": "",
9 | "source": "src"
10 | }
--------------------------------------------------------------------------------
/next/sources/gmachine/src/lazylist/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-4"
3 | }
--------------------------------------------------------------------------------
/next/sources/gmachine/src/part1/instruction.mbt:
--------------------------------------------------------------------------------
1 | // start instr definition
2 | enum Instruction {
3 | Unwind
4 | PushGlobal(String)
5 | PushInt(Int)
6 | PushArg(Int)
7 | MkApp
8 | Update(Int)
9 | Pop(Int)
10 | } derive(Eq, Show)
11 | // end instr definition
12 |
--------------------------------------------------------------------------------
/next/sources/gmachine/src/part1/lazy.mbt:
--------------------------------------------------------------------------------
1 | // start lazy definition
2 | enum LazyData[T] {
3 | Waiting(() -> T)
4 | Done(T)
5 | }
6 |
7 | struct LazyRef[T] {
8 | mut data : LazyData[T]
9 | }
10 |
11 | fn[T] extract(self : LazyRef[T]) -> T {
12 | match self.data {
13 | Waiting(thunk) => {
14 | let value = thunk()
15 | self.data = Done(value) // in-place update
16 | value
17 | }
18 | Done(value) => value
19 | }
20 | }
21 |
22 | fn square(x : LazyRef[Int]) -> Int {
23 | x.extract() * x.extract()
24 | }
25 | // end lazy definition
26 |
--------------------------------------------------------------------------------
/next/sources/gmachine/src/part1/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 |
3 | "warn-list": "-4-8-6"
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/next/sources/gmachine/src/part2/instruction.mbt:
--------------------------------------------------------------------------------
1 | // start instr definition
2 | enum Instruction {
3 | Unwind
4 | PushGlobal(String)
5 | PushInt(Int)
6 | Push(Int)
7 | MkApp
8 | Slide(Int)
9 | Update(Int)
10 | Pop(Int)
11 | Alloc(Int)
12 | Eval
13 | Add
14 | Sub
15 | Mul
16 | Div
17 | Neg
18 | Eq // ==
19 | Ne // !=
20 | Lt // <
21 | Le // <=
22 | Gt // >
23 | Ge // >=
24 | Cond(List[Instruction], List[Instruction])
25 | } derive(Eq, Show)
26 | // end instr definition
27 |
--------------------------------------------------------------------------------
/next/sources/gmachine/src/part2/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-4-8"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/gmachine/src/part3/instruction.mbt:
--------------------------------------------------------------------------------
1 | // start instr definition
2 | enum Instruction {
3 | Unwind
4 | PushGlobal(String)
5 | PushInt(Int)
6 | Push(Int)
7 | MkApp
8 | Slide(Int)
9 | Update(Int)
10 | Pop(Int)
11 | Alloc(Int)
12 | Eval
13 | Add
14 | Sub
15 | Mul
16 | Div
17 | Neg
18 | Eq // ==
19 | Ne // !=
20 | Lt // <
21 | Le // <=
22 | Gt // >
23 | Ge // >=
24 | Cond(List[Instruction], List[Instruction])
25 | Pack(Int, Int) // tag, arity
26 | CaseJump(List[(Int, List[Instruction])])
27 | Split
28 | Print
29 | } derive(Eq, Show)
30 | // end instr definition
31 |
--------------------------------------------------------------------------------
/next/sources/gmachine/src/part3/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-4"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/lambda-expression/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | .mooncakes/
3 |
--------------------------------------------------------------------------------
/next/sources/lambda-expression/README.md:
--------------------------------------------------------------------------------
1 | # moonbit-community/lambda
2 |
3 | Check `tutorial/example/lambda`.
--------------------------------------------------------------------------------
/next/sources/lambda-expression/moon.mod.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "moonbit-community/lambda",
3 | "version": "0.1.0",
4 | "readme": "README.md",
5 | "repository": "",
6 | "license": "Apache-2.0",
7 | "keywords": [],
8 | "description": "",
9 | "source": "src"
10 | }
--------------------------------------------------------------------------------
/next/sources/lambda-expression/src/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-4"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | .mooncakes/
3 |
--------------------------------------------------------------------------------
/next/sources/language/README.md:
--------------------------------------------------------------------------------
1 | # moonbit-community/language
2 |
3 | Covers everything mentioned in `language` section.
--------------------------------------------------------------------------------
/next/sources/language/moon.mod.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "moonbit-community/language",
3 | "version": "0.1.0",
4 | "readme": "README.md",
5 | "repository": "",
6 | "license": "Apache-2.0",
7 | "keywords": [],
8 | "description": "",
9 | "source": "src"
10 | }
--------------------------------------------------------------------------------
/next/sources/language/src/attributes/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "is-main": true,
3 | "warn-list": "-1-2-4-7-9-28"
4 | }
5 |
--------------------------------------------------------------------------------
/next/sources/language/src/benchmark/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-1"
3 | }
--------------------------------------------------------------------------------
/next/sources/language/src/builtin/__snapshot__/byte_1:
--------------------------------------------------------------------------------
1 | 97
2 | 255
3 |
--------------------------------------------------------------------------------
/next/sources/language/src/builtin/__snapshot__/string_1:
--------------------------------------------------------------------------------
1 | 兔
2 | r
3 | Hello
4 | MoonBit\n
5 |
6 |
--------------------------------------------------------------------------------
/next/sources/language/src/builtin/__snapshot__/string_4:
--------------------------------------------------------------------------------
1 | Hello
2 | ---
3 | MoonBit
4 |
5 | ---
6 |
--------------------------------------------------------------------------------
/next/sources/language/src/builtin/__snapshot__/tuple_1:
--------------------------------------------------------------------------------
1 | false 100 text 3.14
2 |
--------------------------------------------------------------------------------
/next/sources/language/src/builtin/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-1-2"
3 | }
--------------------------------------------------------------------------------
/next/sources/language/src/controls/__snapshot__/for_loop_1:
--------------------------------------------------------------------------------
1 | 0
2 | 1
3 | 2
4 | 3
5 | 4
6 |
--------------------------------------------------------------------------------
/next/sources/language/src/controls/__snapshot__/for_loop_4:
--------------------------------------------------------------------------------
1 | even: 2
2 | even: 4
3 | even: 6
4 | 12
5 |
--------------------------------------------------------------------------------
/next/sources/language/src/controls/__snapshot__/for_loop_7:
--------------------------------------------------------------------------------
1 | The 1-th element of the array is 4
2 | The 2-th element of the array is 5
3 | The 3-th element of the array is 6
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/controls/__snapshot__/for_loop_8:
--------------------------------------------------------------------------------
1 | x, 1
2 | z, 3
3 |
--------------------------------------------------------------------------------
/next/sources/language/src/controls/__snapshot__/while_loop_1:
--------------------------------------------------------------------------------
1 | 5
2 | 4
3 | 3
4 | 2
5 | 1
6 |
--------------------------------------------------------------------------------
/next/sources/language/src/controls/__snapshot__/while_loop_2:
--------------------------------------------------------------------------------
1 | 3
2 | 2
3 |
--------------------------------------------------------------------------------
/next/sources/language/src/controls/__snapshot__/while_loop_3:
--------------------------------------------------------------------------------
1 | 2
2 | 1
3 | 0
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/controls/__snapshot__/while_loop_4:
--------------------------------------------------------------------------------
1 | 5
2 |
--------------------------------------------------------------------------------
/next/sources/language/src/controls/__snapshot__/while_loop_5:
--------------------------------------------------------------------------------
1 | 7
2 |
--------------------------------------------------------------------------------
/next/sources/language/src/controls/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-1-2-4-6-7"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/data/__snapshot__/enum_11:
--------------------------------------------------------------------------------
1 | 5
2 | NotImplementedError
3 |
--------------------------------------------------------------------------------
/next/sources/language/src/data/__snapshot__/enum_3:
--------------------------------------------------------------------------------
1 | smaller!
2 | equal!
3 | greater!
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/data/__snapshot__/enum_6:
--------------------------------------------------------------------------------
1 | false
2 | 1,
3 | 2,
4 | nil
5 |
--------------------------------------------------------------------------------
/next/sources/language/src/data/__snapshot__/enum_9:
--------------------------------------------------------------------------------
1 | 0!
2 | 0
3 |
--------------------------------------------------------------------------------
/next/sources/language/src/data/__snapshot__/newtype_2:
--------------------------------------------------------------------------------
1 | 1
2 | John Doe
3 |
--------------------------------------------------------------------------------
/next/sources/language/src/data/__snapshot__/newtype_3:
--------------------------------------------------------------------------------
1 | 1
2 |
--------------------------------------------------------------------------------
/next/sources/language/src/data/__snapshot__/struct_1:
--------------------------------------------------------------------------------
1 | 0
2 | John Doe
3 | john@doe.name
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/data/__snapshot__/struct_4:
--------------------------------------------------------------------------------
1 | { id: 0, name: John Doe, email: john@doe.com }
2 | { id: 0, name: John Doe, email: john@doe.name }
3 |
--------------------------------------------------------------------------------
/next/sources/language/src/data/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-1-2-3-4-6-7-9-28"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/derive/default.mbt:
--------------------------------------------------------------------------------
1 | // start derive default struct
2 | struct DeriveDefault {
3 | x : Int
4 | y : String?
5 | } derive(Default, Eq, Show)
6 |
7 | test "derive default struct" {
8 | let p = DeriveDefault::default()
9 | assert_eq!(p, DeriveDefault::{ x: 0, y: None })
10 | }
11 | // end derive default struct
12 |
13 | // start derive default enum
14 | enum DeriveDefaultEnum {
15 | Case1(Int)
16 | Case2(label~ : String)
17 | Case3
18 | } derive(Default, Eq, Show)
19 |
20 | test "derive default enum" {
21 | assert_eq!(DeriveDefaultEnum::default(), DeriveDefaultEnum::Case3)
22 | }
23 | // end derive default enum
24 |
--------------------------------------------------------------------------------
/next/sources/language/src/derive/hash.mbt:
--------------------------------------------------------------------------------
1 | // start derive hash struct
2 | struct DeriveHash {
3 | x : Int
4 | y : String?
5 | } derive(Hash, Eq, Show)
6 |
7 | test "derive hash struct" {
8 | let hs = @hashset.new()
9 | hs.add(DeriveHash::{ x: 123, y: None })
10 | hs.add(DeriveHash::{ x: 123, y: None })
11 | assert_eq!(hs.size(), 1)
12 | hs.add(DeriveHash::{ x: 123, y: Some("456") })
13 | assert_eq!(hs.size(), 2)
14 | }
15 | // end derive hash struct
16 |
--------------------------------------------------------------------------------
/next/sources/language/src/derive/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-4-6"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/derive/show.mbt:
--------------------------------------------------------------------------------
1 | // start derive show struct
2 | struct MyStruct {
3 | x : Int
4 | y : Int
5 | } derive(Show)
6 |
7 | test "derive show struct" {
8 | let p = MyStruct::{ x: 1, y: 2 }
9 | assert_eq!(Show::to_string(p), "{x: 1, y: 2}")
10 | }
11 | // end derive show struct
12 |
13 | // start derive show enum
14 | enum MyEnum {
15 | Case1(Int)
16 | Case2(label~ : String)
17 | Case3
18 | } derive(Show)
19 |
20 | test "derive show enum" {
21 | assert_eq!(Show::to_string(MyEnum::Case1(42)), "Case1(42)")
22 | assert_eq!(
23 | Show::to_string(MyEnum::Case2(label="hello")),
24 | "Case2(label=\"hello\")",
25 | )
26 | assert_eq!(Show::to_string(MyEnum::Case3), "Case3")
27 | }
28 | // end derive show enum
29 |
--------------------------------------------------------------------------------
/next/sources/language/src/error/__snapshot__/error_9:
--------------------------------------------------------------------------------
1 | division by zero
2 |
--------------------------------------------------------------------------------
/next/sources/language/src/error/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-1-2-3-4-6-15-24-28"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/functions/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-1-2-4-7-9-28"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/generics/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-4"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/generics/top.mbt:
--------------------------------------------------------------------------------
1 | enum List[T] {
2 | Nil
3 | Cons(T, List[T])
4 | }
5 |
6 | fn[S, T] map(self : List[S], f : (S) -> T) -> List[T] {
7 | match self {
8 | Nil => Nil
9 | Cons(x, xs) => Cons(f(x), xs.map(f))
10 | }
11 | }
12 |
13 | fn[S, T] reduce(self : List[S], op : (T, S) -> T, init : T) -> T {
14 | match self {
15 | Nil => init
16 | Cons(x, xs) => xs.reduce(op, op(init, x))
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/next/sources/language/src/is/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-1-28"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/iter/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-1"
3 | }
--------------------------------------------------------------------------------
/next/sources/language/src/iter/top.mbt:
--------------------------------------------------------------------------------
1 | // start iter 1
2 | ///|
3 | fn filter_even(l : Array[Int]) -> Array[Int] {
4 | let l_iter : Iter[Int] = l.iter()
5 | l_iter.filter(fn { x => (x & 1) == 0 }).collect()
6 | }
7 |
8 | ///|
9 | fn fact(n : Int) -> Int {
10 | let start = 1
11 | let range : Iter[Int] = start.until(n)
12 | range.fold(Int::op_mul, init=start)
13 | }
14 | // end iter 1
15 |
16 | // start iter 2
17 | ///|
18 | fn iter(data : Bytes) -> Iter[Byte] {
19 | Iter::new(fn(visit : (Byte) -> IterResult) -> IterResult {
20 | for byte in data {
21 | guard visit(byte) is IterContinue else { break IterEnd }
22 |
23 | } else {
24 | IterContinue
25 | }
26 | })
27 | }
28 | // end iter 2
29 |
--------------------------------------------------------------------------------
/next/sources/language/src/main/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "is-main": true
3 | }
--------------------------------------------------------------------------------
/next/sources/language/src/main/top.mbt:
--------------------------------------------------------------------------------
1 | // start init
2 | fn init {
3 | let x = 1
4 | println(x)
5 | }
6 | // end init
7 |
8 | // start main
9 | fn main {
10 | let x = 2
11 | println(x)
12 | }
13 | // end main
14 |
--------------------------------------------------------------------------------
/next/sources/language/src/method/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-1-2-4-6-7-13-28"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/method2/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "import": [
3 | {
4 | "path": "moonbit-community/language/method",
5 | "alias": "list"
6 | }
7 | ],
8 | "warn-list": "-1-2-4-6-28"
9 | }
10 |
--------------------------------------------------------------------------------
/next/sources/language/src/misc/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-1-2-28"
3 | }
--------------------------------------------------------------------------------
/next/sources/language/src/misc/top.mbt:
--------------------------------------------------------------------------------
1 | // start doc string 1
2 | /// Return a new array with reversed elements.
3 | ///
4 | /// # Example
5 | ///
6 | /// ```
7 | /// reverse([1,2,3,4]) |> println()
8 | /// ```
9 | fn[T] reverse(xs : Array[T]) -> Array[T] {
10 | ...
11 | }
12 | // end doc string 1
13 |
14 | // start todo 1
15 | fn todo_in_func() -> Int {
16 | ...
17 | }
18 | // end todo 1
19 |
--------------------------------------------------------------------------------
/next/sources/language/src/operator/__snapshot__/operator_3:
--------------------------------------------------------------------------------
1 | {x: 1, y: 2}
2 | 2
3 | {x: 23, y: 2}
4 | 23
5 |
--------------------------------------------------------------------------------
/next/sources/language/src/operator/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-1-2-4-11"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/packages/implement/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "implement": "moonbit-community/language/packages/virtual"
3 | }
--------------------------------------------------------------------------------
/next/sources/language/src/packages/implement/top.mbt:
--------------------------------------------------------------------------------
1 | pub fn log(string : String) -> Unit {
2 | ignore(string)
3 | }
--------------------------------------------------------------------------------
/next/sources/language/src/packages/pkgA/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {}
--------------------------------------------------------------------------------
/next/sources/language/src/packages/pkgA/top.mbt:
--------------------------------------------------------------------------------
1 | pub fn incr(x : Int) -> Int {
2 | x + 1
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/packages/pkgB/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "import": [
3 | "moonbit-community/language/packages/pkgA",
4 | {
5 | "path": "moonbit-community/language/packages/pkgC",
6 | "alias": "c"
7 | }
8 | ]
9 | }
--------------------------------------------------------------------------------
/next/sources/language/src/packages/pkgB/top.mbt:
--------------------------------------------------------------------------------
1 | pub fn add1(x : Int) -> Int {
2 | @moonbitlang/core/int.abs(@c.incr(@pkgA.incr(x)))
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/packages/pkgC/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {}
--------------------------------------------------------------------------------
/next/sources/language/src/packages/pkgC/top.mbt:
--------------------------------------------------------------------------------
1 | pub fn incr(x : Int) -> Int {
2 | x + 1
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/packages/use_implement/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "overrides": ["moonbit-community/language/packages/implement"],
3 | "import": [
4 | "moonbit-community/language/packages/virtual"
5 | ],
6 | "is-main": true
7 | }
--------------------------------------------------------------------------------
/next/sources/language/src/packages/use_implement/top.mbt:
--------------------------------------------------------------------------------
1 | fn main {
2 | @virtual.log("Hello")
3 | }
--------------------------------------------------------------------------------
/next/sources/language/src/packages/virtual/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "virtual": {
3 | "has-default": true
4 | }
5 | }
--------------------------------------------------------------------------------
/next/sources/language/src/packages/virtual/top.mbt:
--------------------------------------------------------------------------------
1 | pub fn log(s : String) -> Unit {
2 | println(s)
3 | }
--------------------------------------------------------------------------------
/next/sources/language/src/packages/virtual/virtual.mbti:
--------------------------------------------------------------------------------
1 | package "moonbit-community/language/packages/virtual"
2 |
3 | fn log(String) -> Unit
--------------------------------------------------------------------------------
/next/sources/language/src/pattern/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-1-2-4-6-7-9-10-11-12-28"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/test/__snapshot__/record_anything.txt:
--------------------------------------------------------------------------------
1 | Hello, world! And hello, MoonBit!
2 |
--------------------------------------------------------------------------------
/next/sources/language/src/test/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-3-4"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/trait/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-1-2-3-4-5-6-9-28"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/language/src/variable/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "is-main": true
3 | }
--------------------------------------------------------------------------------
/next/sources/language/src/variable/top.mbt:
--------------------------------------------------------------------------------
1 | let zero = 0
2 |
3 | const ZERO = 0
4 |
5 | fn main {
6 | //! const ZERO = 0
7 | let mut i = 10
8 | i = 20
9 | println(i + zero + ZERO)
10 | }
11 |
--------------------------------------------------------------------------------
/next/sources/segment-tree/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | .mooncakes/
3 |
--------------------------------------------------------------------------------
/next/sources/segment-tree/README.md:
--------------------------------------------------------------------------------
1 | # Segment Tree
2 |
3 | Check `tutorial/example/segment-tree`.
--------------------------------------------------------------------------------
/next/sources/segment-tree/moon.mod.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "moonbit-community/segment-tree",
3 | "version": "0.1.0",
4 | "readme": "README.md",
5 | "repository": "",
6 | "license": "Apache-2.0",
7 | "keywords": [],
8 | "description": "",
9 | "source": "src"
10 | }
--------------------------------------------------------------------------------
/next/sources/segment-tree/src/part1/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-4"
3 | }
4 |
--------------------------------------------------------------------------------
/next/sources/segment-tree/src/part2/moon.pkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "warn-list": "-4"
3 | }
4 |
--------------------------------------------------------------------------------
/next/toolchain/index.md:
--------------------------------------------------------------------------------
1 | # Toolchains
2 |
3 | Here are some manuals that may help you use the toolchains of the programming language:
4 |
5 | - [MoonBit's Build System](./moon/index.md): full manual of `moon` build system.
6 | - VSCode extension
7 | - ...
8 |
9 | ```{only} html
10 | [Download this section in Markdown](path:/download/toolchain/summary.md)
11 | ```
12 |
13 | ```{toctree}
14 | :maxdepth: 2
15 | :caption: Contents:
16 | moon/index
--------------------------------------------------------------------------------
/next/toolchain/moon/index.md:
--------------------------------------------------------------------------------
1 | # Moon Build System
2 |
3 | ```{toctree}
4 | :maxdepth: 2
5 | :caption: Contents:
6 | tutorial
7 | package-manage-tour
8 | commands
9 | module
10 | package
11 | coverage
12 | ```
13 |
--------------------------------------------------------------------------------
/next/tutorial/index.md:
--------------------------------------------------------------------------------
1 | # Tutorial
2 |
3 | Here are some tutorials that may help you learn the programming language:
4 |
5 | - [An interactive tour with language basics](https://tour.moonbitlang.com)
6 | - [Tour for Beginners](./tour.md)
7 |
8 | ```{only} html
9 | [Download this section in Markdown](path:/download/tutorial/summary.md)
10 | ```
11 |
12 | ```{toctree}
13 | :hidden:
14 | tour
15 | for-go-programmers/index
16 | ```
17 |
18 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "packageManager": "pnpm@9.15.0+sha512.76e2379760a4328ec4415815bcd6628dee727af3779aaa4c914e3944156c4299921a89f976381ee107d41f12cfa4b66681ca9c718f0668fa0831ed4c6d8ba56c"
3 | }
4 |
--------------------------------------------------------------------------------
/pnpm-workspace.yaml:
--------------------------------------------------------------------------------
1 | packages:
2 | - "moonbit-tour"
3 |
--------------------------------------------------------------------------------
/typos.toml:
--------------------------------------------------------------------------------
1 | [files]
2 | extend-exclude = ["*.excalidraw"]
3 |
4 | [default]
5 | extend-ignore-words-re = ["CAF", "ND", "/Fo||/Fe"]
--------------------------------------------------------------------------------