Imports
The import system allows you to organize code across multiple files and reuse functions and variables from other modules.
Import Statement
Import statements load code from other DLiteScript files.
Syntax
| |
Basic Import
Import a file using a relative or absolute path. The filename (without extension) becomes the namespace:
| |
In this example:
- Functions and variables from
utils.dlare accessed via theutilsnamespace utils.add()calls theaddfunction from that file
Namespaces
By default, imports create a namespace based on the filename.
Accessing Imported Items
Use the namespace followed by a dot to access functions and variables:
| |
File Structure
utils.dl:
| |
main.dl:
| |
Import Aliases
You can specify a custom namespace using the as keyword.
Custom Namespace
| |
Global Import
Use _ as the alias to import into the global scope:
| |
With as _, all functions and variables are imported directly into the global scope.
Import Paths
Relative Paths
Relative paths are resolved relative to the current file:
| |
Absolute Paths
You can also use absolute paths:
| |
Module Organization
Practical Example
Project structure:
project/
main.dl
utils.dl
test.dlutils.dl:
| |
test.dl:
| |
main.dl:
| |
Important Notes
File Extension
By convention, DLiteScript files use the .dl extension:
| |
Import Order
Imports are evaluated when the import statement is encountered. It’s common practice to place all imports at the top of the file.
Improve this page