From 0e85add924fe7cfc141ba6f9ec4437483ecb59ce Mon Sep 17 00:00:00 2001 From: zihang Date: Mon, 16 Dec 2024 17:40:55 +0800 Subject: [PATCH] doc: fix code --- next/language/methods.md | 6 +++--- next/sources/language/src/method/top.mbt | 4 ++-- next/sources/language/src/method2/top.mbt | 6 ++++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/next/language/methods.md b/next/language/methods.md index 3b46d359..291d8c56 100644 --- a/next/language/methods.md +++ b/next/language/methods.md @@ -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`: diff --git a/next/sources/language/src/method/top.mbt b/next/sources/language/src/method/top.mbt index 39e5724e..ac5419b6 100644 --- a/next/sources/language/src/method/top.mbt +++ b/next/sources/language/src/method/top.mbt @@ -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 diff --git a/next/sources/language/src/method2/top.mbt b/next/sources/language/src/method2/top.mbt index 9f0b9f57..5f38bcc3 100644 --- a/next/sources/language/src/method2/top.mbt +++ b/next/sources/language/src/method2/top.mbt @@ -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] { ...