Skip to content

Commit

Permalink
doc: introduce package and modules
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-jerry-ye committed Dec 16, 2024
1 parent 8195711 commit 68e03b1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
24 changes: 24 additions & 0 deletions next/language/packages.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# Managing Projects with Packages

When developing projects at large scale, the project usually needs to be divided into smaller modular unit that depends on each other.
More often, it involves using other people's work: most noticeably is the [core](https://github.com/moonbitlang/core), the standard library of MoonBit.

## Packages and modules

In MoonBit, the most important unit for code organization is a package, which consists of a number of source code files and a single `moon.pkg.json` configuration file.
A package can either be a `main` package, consisting a `main` function, or a package that serves as a library.

A project, corresponding to a module, consists of multiple packages and a single `moon.mod.json` configuration file.

When using things from another package, the dependency between modules should first be declared inside the `moon.mod.json`.
The dependency between packages should then be declared inside the `moon.pkg.json`.
Then it is possible to use `@pkg` to access the imported entities, where `pkg` is the last part of the imported package's path or the declared alias in `moon.pkg.json`:

```{literalinclude} /sources/language/src/packages/pkgB/moon.pkg.json
:language: json
:caption: pkgB/moon.pkg.json
```

```{literalinclude} /sources/language/src/packages/pkgB/top.mbt
:language: moonbit
:caption: pkgB/top.mbt
```

## Access Control

By default, all function definitions and variable bindings are _invisible_ to other packages.
Expand Down
1 change: 1 addition & 0 deletions next/sources/language/src/packages/pkgA/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions next/sources/language/src/packages/pkgA/top.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn incr(x : Int) -> Int {
x + 1
}
5 changes: 5 additions & 0 deletions next/sources/language/src/packages/pkgB/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"import": [
"moonbit-community/language/packages/pkgA"
]
}
3 changes: 3 additions & 0 deletions next/sources/language/src/packages/pkgB/top.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn add1(x : Int) -> Int {
@pkgA.incr(x)
}

0 comments on commit 68e03b1

Please sign in to comment.