Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Sections

A sect is a named scope. :: reaches into it. cite brings a name into the current scope and does not run it. A declaration inside a section is private unless it is marked pub. A declaration in no section is visible everywhere, which is why a single-file program can ignore sections and why the prelude is the prelude.

// A section keeps a helper private and publishes the rest.

sect geometry {
    pub enum Shape {
        Circle(i64),
        Rect(i64, i64),
    }

    func squared(n: i64) -> i64 { <(n, n) | mul }

    pub func area(s: Shape) -> i64 {
        of s {
            Circle(r) => <(3, <r | squared) | mul,
            Rect(w, h) => <(w, h) | mul,
        }
    }
}

cite geometry::area;

proc main | (exit: i32) / {IO} {
    <geometry::Shape::Circle(5) | area | println;
    <geometry::Shape::Rect(6, 7) | area | println;
    <0 | exit>
}
75
42

Inside geometry, Shape and squared are bare names. From main, the enum is geometry::Shape and the public function can be cited:

cite geometry::area;
<geometry::Shape::Circle(5) | area | println;

squared has no pub, so geometry::squared from main is refused. Privacy is the section and the sections nested in it.

cite geometry::Shape::*; brings the public variants in bare. cite geometry::*; brings every public member. A glob is the weakest way a name arrives: an explicit cite and the section's own declarations win. Two globs may offer the same name, and that is reported when the name is used.

A program's declaration of a prelude name shadows the prelude name.

A section in its own file

A sect with no body names a file:

sect shapes;
pub sect report;

The file holds the declarations themselves, with no sect shapes { … } around them. The directory tree is the section tree.

The declaration is insect name; reads
the program, dir/main.sldir/name.sl
a section file, dir/m.sldir/m/name.sl
an inline section sect a { … }one a/ further down

There is no search path. Run and check the root file. The sample is examples/programs/multi_file/.

main is declared at the root. A main inside a section is that::main, and the entry point does not accept it.

The standard library is a set of sections, one per file: list, map, fs, and the rest. Nothing in them is in scope until the program names it. The next chapter does.