Skip to content

Commit

Permalink
doc(book): explain the schema importing mechanism
Browse files Browse the repository at this point in the history
This section was still empty and despite even now being somewhat
incomplete, at least provides some basic explanation of the importing
system.
  • Loading branch information
dnaka91 committed Feb 24, 2024
1 parent 5cba463 commit c6b3688
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
28 changes: 26 additions & 2 deletions book/src/reference/schema/imports.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# Imports

## Schema
When splitting data structures into multiple Schema files, imports are used to reference the types defined within. Imports are declared with the `use` statement.

<<< imports/basic.mabo
These come in two flavors:

- Import a specific type.

<<< imports/struct.mabo

- Import only a module.

<<< imports/module.mabo

Individual elements forming the import path are separated by a double-colon `::`. The first element is name of the external schema, and all intermediate elements are modules.

The last element is either omitted, which results in bringing the whole module into scope, or a specific type in that module or root schema.

Importing a module can help to reduce repetition if the module path is deep. Another use case is the avoidance of duplicate type names. For example:

## Scoping example

A simple example with a somewhat deeply nested module structure, which we'll use in the next step. Let's consider this file to be named `other.mabo`.

<<< imports/other.mabo

When importing the above schema, we bring the `addresses` module into scope and can reference all the contained types:

<<< imports/scoping.mabo
1 change: 0 additions & 1 deletion book/src/reference/schema/imports/basic.mabo

This file was deleted.

1 change: 1 addition & 0 deletions book/src/reference/schema/imports/module.mabo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use other::my_module;
8 changes: 8 additions & 0 deletions book/src/reference/schema/imports/other.mabo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod users {
mod addresses {
struct Street {
name: string @1,
house_no: string @2,
}
}
}
5 changes: 5 additions & 0 deletions book/src/reference/schema/imports/scoping.mabo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use other::users::addresses;

struct MyStruct {
street: addresses::Street @1,
}
1 change: 1 addition & 0 deletions book/src/reference/schema/imports/struct.mabo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use other::my_module::MyStruct;

0 comments on commit c6b3688

Please sign in to comment.