Modules & Paths
Categories:
Using Modules to Organize Code
Modules
A module is a purely organizational structure in Bramble. They allow you to group related functions and types together under a shared name.
Accessibility
The concepts of public and private and accessibility are not yet implemented in Bramble.
Default Modules
In Bramble, there are a few default modules that always exist and help to organize your code.
- The
project
module. This is the parent module and corresponds to your project as a whole. There is only oneproject
module in any given project. Theproject
module is also what you import when you import another Bramble project into your code. - The
file
module. The file module is a module that represents a source code file and it has a name that is the same as the file name. directory
modules. If there are subdirectories in your project, those will be added asmodules
which contain all the times in those subdirectories.
Custom Modules
Within your own code, you can use mod
to create modules that fall
under the file
module.
Using Paths
There are two types of paths in Bramble: relative and canonical. The relative path is a path that gives directions to an item starting from the current module, this path is dependent upon where it is used. The canonical path starts from the project root. This path will always point to the same item no matter where in the source code it is used.
Path Keywords
There are several path keywords.
project
: all canonical paths begin with theproject
keyword.self
: self is an alias for the current module.super
: move to the current modules parent module.
Example
Here is an example of a module with another module in it:
mod my_mod {
fn is_3(i: i64) -> bool {
let x: i64 := self::inner::add(1, 2);
return i == x;
}
fn plus(a: i64, b: i64) -> i64 {
return a + b;
}
mod inner {
fn add(a: i64, b: i64) -> i64 {
return super::plus(a, b);
}
}
}
If this source code were contained in the project: example
in the
file foo.br
, then the project module is example
and the file
module is foo
. The canonical path to add
is
project::example::foo::my_mod::inner::add
.
In this example, self::inner::add
and super::plus
represent
relative paths.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.