5.1.16. Modules and Program Structure
This tutorial covers the file layout (options, module, require), creating
and using a separate module file, public vs private visibility, qualified
calls with ::, and common standard library modules.
5.1.16.1. File layout
A typical daslang file follows this order:
options gen2 // compiler options
module my_module public // module declaration
require math // imports
// ... declarations (struct, enum, def, etc.)
The module line must appear before any declarations.
5.1.16.2. Module declaration
The module keyword names the current file. Omit it to default to the
filename:
module my_lib public // public declarations by default
module my_lib private // private declarations by default
module my_lib shared // shared across contexts
5.1.16.3. Creating a module
Create a file tutorial_helpers.das next to your main script:
options gen2
module tutorial_helpers public
struct Item {
name : string
value : int
}
def make_item(n : string; v : int) : Item {
return Item(name=n, value=v)
}
def describe(item : Item) : string {
return "{item.name} (worth {item.value})"
}
def private internal_id(item : Item) : int {
return item.value * 31
}
let MAX_ITEMS = 100
5.1.16.4. Using a module
In the main file, import and use it:
require tutorial_helpers
let sword = make_item("Sword", 150)
print("{describe(sword)}\n")
print("max: {MAX_ITEMS}\n")
If the module file is in the same directory, require finds it by name.
5.1.16.5. Qualified calls
Use module::function to disambiguate:
let shield = tutorial_helpers::make_item("Shield", 80)
let r = math::sqrt(16.0)
This is essential when two modules export the same function name.
5.1.16.6. Visibility
Each declaration can override the module default:
def public helper() { ... } // always visible
def private internal() { ... } // only within this module
Private functions like internal_id() above cannot be called from
other modules — the compiler reports an error.
5.1.16.7. Re-exporting
require ... public re-exports a module to your importers:
require math public // importers of this module also get math
5.1.16.8. Common standard library modules
Module |
Purpose |
|---|---|
|
sin, cos, sqrt, atan2, etc. |
|
to_int, to_float, starts_with, etc. |
|
File I/O |
|
Runtime type information |
|
JSON parsing |
|
Regular expressions |
|
Sorting and searching |
|
Advanced string utilities |
See also
Modules, Program Structure in the language reference.
Full source: tutorials/language/16_modules.das and
tutorials/language/tutorial_helpers.das
Next tutorial: Move, Copy, and Clone