Imports: programs of several files
A program can use functions and values from other .luma files. There is no main function and no special main file: the file you run is the program, and the files it imports bring their parts.
File greetings.luma:
pub fn hello(name: str): str {
return "Hello, ${name}!"
}
pub fn bye(name: str): str {
return "Goodbye, ${name}."
}A program that imports it:
import "greetings"
print(greetings.hello("Ann"))
print(greetings.bye("Ann"))Output:
Hello, Ann!
Goodbye, Ann.import "greetings" reads greetings.luma from the same folder. Its pub functions and variables are then used through the file’s name: greetings.hello(...).
One library, several programs
Because the file you run is the program, several programs can share the same files. With report.luma and backup.luma both importing tools.luma:
luma run report.luma # one program
luma run backup.luma # another program, using the same tools.lumapub: what a file shares
Everything in a file is private to it, unless it is marked pub. pub goes before top-level functions, variables, objs, traits and locked values (pub lock PI: float = 3.14):
File shop.luma:
pub currency: str = "EUR"
pub prices: {str: int} = {"apple": 2, "bread": 3}
pub fn price(item: str): str {
return "${cents(item)} cents"
}
fn cents(item: str): int {
return 100
}import "shop"
print(shop.currency)
print(shop.prices["bread"])
print(shop.price("apple"))Output:
EUR
3
100 centsUsing something that isn’t pub is a compile error that says where to add it:
print(shop.cents("apple")) // error: cents in shop.luma is not pub; to use it here, define it there as pub fn cents(...)A file’s pub variables can be read by the files that import it, but only the file itself can assign to them:
shop.currency = "USD" // error: shop.currency belongs to shop.luma: only that file can change itA pub list, map or set is shared, as everywhere in Luma: an importer can change what is inside it (shop.prices["milk"] = 1).
Paths
The path is relative to the file that contains the import, and is written without .luma, with / between folders on every operating system:
import "greetings" // greetings.luma, next to this file
import "lib/text" // lib/text.luma, in the folder lib
import "../common/tools" // tools.luma, one folder up and then in commonThe name in the code is the file’s name: text for "lib/text". To use another name, or when the file name can’t be a name (like my-tools.luma), use as:
import "../common/my-tools" as toolsImports go at the top of a file, before its other statements.
Inline imports
For a single use, a file can be imported right where it is needed, in parentheses:
print((import "greetings").hello("Bo"))
fn farewell(name: str): str {
return (import "greetings").bye(name)
}
print(farewell("Bo"))Output:
Hello, Bo!
Goodbye, Bo.An inline import is the same as one at the top: the file is read and set up once, before the program starts, however many times and wherever it is imported.
How imported files run
A file can have top-level code too, for example to fill its pub variables. When a program starts:
- Every imported file is set up first, before the code of the file that imports it, in the order the imports are written. If an imported file imports others, those come before it.
- Each file is set up only once, even when several files import it. They then share it, including its variables.
- Then the program’s own code runs.
So a file that is meant to be imported shouldn’t print anything at its top level, because that would happen in every program that imports it.
Two files can’t import each other, directly or through others:
circular import: a.luma -> b.luma -> a.lumaMove what both need into a third file that both import.
Names don’t clash
Each file has its own names: a function area in main.luma and one in shapes.luma are different functions, area(...) and shapes.area(...). A file’s functions don’t see the variables of its own top level (see Functions), but they can use the pub variables of the files it imports.
Errors in an imported file name that file:
shapes.luma:4:12: runtime error: division by zeroGetting mods from the web: luma get
A mod is one or more .luma files that someone shares, like Luma’s official database drivers. luma get downloads one into the current folder, with every file it imports:
luma get https://raw.githubusercontent.com/lumalang/awesome-luma/main/mods/sqlite.lumanew sqlite.luma
new sqlite/store.luma
new sqlite/record.luma
...luma get reads each file’s imports and fetches them from beside it on the web, just as Luma finds them beside it on disk, so the folders come out as the mod has them, and a program in this folder can import "sqlite". Any web server that serves the files can host a mod: yours, a colleague’s or Luma’s.
Run it again to update. Each file is compared with the one already there:
| The file here | What happens |
|---|---|
| Missing | Written: new |
| The same | Skipped: same |
| Different | You’re asked: sqlite/pager.luma exists and differs; overwrite? [y/n/a] (a: yes to all) |
Without a terminal to ask (in a script), a file that differs is kept, luma get says so, and its exit code is 1. luma get -y <url> overwrites them without asking.
Nothing is written until every file has been fetched and checked, so a broken download leaves the folder as it was. Each file must be:
- Luma source: text (UTF-8), not a program, a picture or a web page, at most 4 MB, and code that parses.
- A
.lumafile whose imports stay in the mod’s folder: no/etc/..., nor a../that leads out of it. - From an
https://address (http://only to this computer,localhost).
A mod is code that runs as your program, so get mods from people you trust, and read what they do.
Not available yet
- A standard library to import (the original Luma had
csv,table, …). - Importing only some names of a file.
luma getwith a mod’s short name (luma get sqlitefor Luma’s official mods) and versions pinned in a lock file. For now, give the full URL.
See Limitations.