Modules & Paths

Using Modules to Organize Code

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 one project module in any given project. The project 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 as modules 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 the project 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.


Last modified February 7, 2022: Add RFCs to the documentation site (5ab3587)