diff --git a/book/src/reference/schema/imports.md b/book/src/reference/schema/imports.md index 0408985..088d36e 100644 --- a/book/src/reference/schema/imports.md +++ b/book/src/reference/schema/imports.md @@ -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 diff --git a/book/src/reference/schema/imports/basic.mabo b/book/src/reference/schema/imports/basic.mabo deleted file mode 100644 index da8b7a2..0000000 --- a/book/src/reference/schema/imports/basic.mabo +++ /dev/null @@ -1 +0,0 @@ -use other::schema::Sample; diff --git a/book/src/reference/schema/imports/module.mabo b/book/src/reference/schema/imports/module.mabo new file mode 100644 index 0000000..51d106b --- /dev/null +++ b/book/src/reference/schema/imports/module.mabo @@ -0,0 +1 @@ +use other::my_module; diff --git a/book/src/reference/schema/imports/other.mabo b/book/src/reference/schema/imports/other.mabo new file mode 100644 index 0000000..34714ed --- /dev/null +++ b/book/src/reference/schema/imports/other.mabo @@ -0,0 +1,8 @@ +mod users { + mod addresses { + struct Street { + name: string @1, + house_no: string @2, + } + } +} diff --git a/book/src/reference/schema/imports/scoping.mabo b/book/src/reference/schema/imports/scoping.mabo new file mode 100644 index 0000000..a230506 --- /dev/null +++ b/book/src/reference/schema/imports/scoping.mabo @@ -0,0 +1,5 @@ +use other::users::addresses; + +struct MyStruct { + street: addresses::Street @1, +} diff --git a/book/src/reference/schema/imports/struct.mabo b/book/src/reference/schema/imports/struct.mabo new file mode 100644 index 0000000..497a262 --- /dev/null +++ b/book/src/reference/schema/imports/struct.mabo @@ -0,0 +1 @@ +use other::my_module::MyStruct;