Pelago
Modules and imports

Imports

import and use: every form (module, renamed module, some names, one renamed name, everything), name clashes, own names winning, and cycles.

An import is a binding, written like the declarations around it. It goes at the top level of a file, not inside a function.

FormEffect
import std.osThe module, under its last name: os.exit(1).
import tools = std.osThe module under another name: tools.exit(1).
import { exit } = std.osSome names, directly: exit(1).
import { exit: leave } = std.osOne name, renamed.
import { ... } = std.osEverything the module makes public.
pub import { exit } = std.osImports the names and re-exports them; see Re-exporting.

use is another spelling of import.

geometry.pel:

pub fn origin() -> int = 0
pub const scale: int = 3

main.pel:

import geometry
import { origin: zero, scale } = geometry

fn main() {
    print(zero(), scale, geometry.scale)
}

pelago run . prints:

0 3 3

When two imports clash

Two imports may bring the same name. It is an error only when you use that name, and the message tells you how to choose:

a.pel:

pub fn fa() -> int = 1

b2.pel:

pub fn fa() -> int = 9

main.pel:

import { fa } = a
import { fa } = b2

fn main() {
    print(fa())
}

pelago run . stops with:

main:5:11: error: `fa` could be `main.a.fa` and `main.b2.fa`; write the module name in front to choose

Write a.fa() or rename one: import { fa: fa2 } = b2.

Your own names win

A function declared in your module takes the place of an imported name with no error:

a.pel:

pub fn fa() -> int = 1

main.pel:

import { fa } = a

fn fa() -> int = 5

fn main() {
    print(fa())
}

pelago run . prints:

5

Cycles

Modules may import each other, so two files can call into one another:

a.pel:

import b

pub fn fa() -> int = 1
pub fn useb() -> int = b.fb()

b.pel:

import a

pub fn fb() -> int = 2
pub fn usea() -> int = a.fa()

main.pel:

use a
use { ... } = b

fn main() {
    print(a.useb(), usea(), fb())
}

pelago run . prints:

2 1 2

On this page