Skip to content

Commit

Permalink
doc: fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-jerry-ye committed Dec 16, 2024
1 parent c0ed77c commit 0e85add
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
6 changes: 3 additions & 3 deletions next/language/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

MoonBit supports methods in a different way from traditional object-oriented languages. A method in MoonBit is just a toplevel function associated with a type constructor. Methods can be defined using the syntax `fn TypeName::method_name(...) -> ...`:

```{literalinclude} /sources/language/src/method/top.mbt
```{literalinclude} /sources/language/src/method2/top.mbt
:language: moonbit
:start-after: start method 1
:end-before: end method 1
:start-after: start method 7
:end-before: end method 7
```

As a convenient shorthand, when the first parameter of a function is named `self`, MoonBit automatically defines the function as a method of the type of `self`:
Expand Down
4 changes: 2 additions & 2 deletions next/sources/language/src/method/top.mbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// start method 1
enum List[X] {
pub(all) enum List[X] {
Nil
Cons(X, List[X])
}

fn List::concat[X](xs : List[List[X]]) -> List[X] {
pub fn List::concat[X](xs : List[List[X]]) -> List[X] {
...
}
// end method 1
Expand Down
6 changes: 6 additions & 0 deletions next/sources/language/src/method2/top.mbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
// start method 7
enum List[X] {
Nil
Cons(X, List[X])
}

fn List::concat[X](xs : List[List[X]]) -> List[X] {
...
}
// end method 7

// start method 3
fn map[X, Y](self : List[X], f : (X) -> Y) -> List[Y] {
...
Expand Down

0 comments on commit 0e85add

Please sign in to comment.