Every Captain file belongs to a module, and every name in a program is either declared in its module, imported from another one, or local to a procedure body. This chapter gives the rules for each, and the reason most of them are stricter than in other languages: a reader of a Captain program should be able to tell where any name comes from by looking at the line it is on.
The module line
A file starts with module and a path of capitalized segments joined by
::, ended by a .:
module Shop::Prices.
export unit -> Decimal [ ret 1.00. ].Nothing else may come first, not even another declaration. The path is the
module's identity: it names the module in diagnostics, in captain inspect,
and in the identities of its types at run time. It does not have to match
the file's name or directory. The compiler finds modules by what files
declare, never by where they are.
use
A module imports another with use and the other module's full path. Every
use comes straight after the module line, before any declaration:
module Shop::App.
use Shop::Prices.
use Std::Json.That is the start of a file. A use after a declaration is refused with
CAP-PARSE-USE.
What a use gives depends on what kind of module it names. A source
module is an ordinary .capt file, yours or one under the repository's
lib/. A compiler-known module such as Std::Json or Std::Text has no
source: the compiler supplies its names.
Using library modules
and Compiler-known modules
describe each compiler-known module's names. The rest of this chapter is
about source modules.
Calling another module: qualified names
A use binds the last segment of the module's path. Everything you take
from that module is written through that name: procedures as
Prices::total of: line, types as Prices::Line.
Here is a small library module:
module Shop::Prices.
export struct Line { price: Decimal, quantity: i32 }.
export line price: Decimal quantity: i32 -> Line [
ret Line { price: price, quantity: quantity }.
].
export total of: Line -> Decimal [
ret (of price) * (multiplier n: (of quantity)).
].
;; Not exported: only this module can call it.
multiplier n: i32 -> Decimal [
ret n as: Decimal.
].And a module that uses it:
module Shop::App.
use Shop::Prices.
export cost price: Decimal quantity: i32 -> Decimal [
l := Prices::line price: price quantity: quantity.
ret Prices::total of: l.
].
export single price: Decimal -> Prices::Line [
ret Prices::line price: price quantity: 1.
].An unqualified call does not resolve, even though the module is imported:
module Shop::App.
use Shop::Prices.
export cost price: Decimal -> Decimal [
ret total of: (line price: price quantity: 1).
].Neither does the full path, Shop::Prices::total of: l. There is exactly one
way to write a call into another module, and it names the module at the
call.
This is deliberate. A program that is mostly read, and often written by a machine, should not make the reader search the imports to learn which module a call goes to. The qualifier is short, and it is on every line that crosses a module boundary.
Two imports whose paths end in the same segment would bind the same name,
so they are refused together (CAP-MODULE-003). Two modules that import
each other, directly or through others, are refused as a cycle
(CAP-MODULE-CYCLE): put what both need in a third module.
Exports and privacy
A declaration is private to its module unless it is marked export. That
holds for procedures, structs, enums, aliases, protocols, instances and
effects. In Shop::Prices, multiplier n: has no export, so another
module cannot call it:
module Shop::App.
use Shop::Prices.
export asDecimal n: i32 -> Decimal [
ret Prices::multiplier n: n.
].The message is `Prices::multiplier n:` is not exported by the imported
module.
An exported struct can be named, passed around and read in another module:
(l price) works on a Prices::Line. It cannot be built there. A record
literal takes an unqualified type name, and Prices::Line { ... } does not
parse. A module whose structs callers need to build exports a procedure that
builds them, as Prices::line price:quantity: does. Variants of an imported
enum cannot be named either; the same kind of procedure makes them.
Structs and enums has
more.
Checking and building more than one module
captain check checks the files it is given, together:
$ captain check app.capt prices.capt
ok prices.capt (module Shop::Prices, 4 declarations)
ok app.capt (module Shop::App, 2 declarations)
checked 2 files as 2 modules, no errorscaptain build takes one root file and an include directory, -I. It
reads every .capt file beneath that directory, matches each use against
the modules the files declare, and links the modules the root reaches into
one artifact:
$ mkdir -p shop && cp prices.capt shop/
$ captain build app.capt -I shop -o app.capb
built app.capb ([..] bytes, sha256 [..])
$ captain run app.capb cost price=2.50 quantity=3
run log: cost.runlog
7.50
$ captain run app.capb single price=1.25
run log: single.runlog
Shop::Prices::Line { price: 1.25, quantity: 1 }The artifact carries the library code it uses; nothing is loaded at run
time, and the artifact's digest covers the library too. A struct from
another module prints with its full module path, because that is its
identity at run time: two modules may both declare a Line, and they are
different types.
A module in several files
A module may be split across files. Each file starts with the same module
line, and each declaration belongs to exactly one of them. The files see
each other's declarations, exported or not:
module Geo::Shapes.
export struct Rect { width: i32, height: i32 }.
export area of: Rect -> i32 [
ret (of width) * (of height).
].module Geo::Shapes.
export square side: i32 -> i32 [
ret area of: Rect { width: side, height: side }.
].$ captain check shapes-a.capt shapes-b.capt
ok shapes-a.capt (module Geo::Shapes, 2 declarations)
ok shapes-b.capt (module Geo::Shapes, 1 declaration)
checked 2 files as 1 module, no errorsDeclaring the same name in two files of one module is CAP-MODULE-002. The
order of the files never changes the result. To build a module split this
way, put the other fragments in the -I directory, not the root file
itself.
Local names
Inside a procedure, names are parameters and locals.
A parameter is named by its selector. line price: Decimal quantity: i32
binds price and quantity in the body. There is no separate internal
name.
The first := declares. total := 0. declares total, with the type of
its initial value. A later total := total + 1. in its scope assigns it, and
the new value must have the same type. There is no separate declaration
statement and no type annotation on a local: the initial value says what it
is.
A local lives until the end of the block that declares it. A block
nested inside the body, such as the body of a forEach: or an ifTrue:, can
read and assign the locals around it. A name first assigned inside a nested
block disappears when that block ends:
module Example::Scope.
export pick flag: bool -> i32 [
flag ifTrue: [ chosen := 1 ].
ret chosen.
].chosen might never have been assigned, so it is not in scope at the
ret. Declare it before the branch, and assign it inside:
module Example::Scope.
export pick flag: bool -> i32 [
chosen := 0.
flag ifTrue: [ chosen := 1 ].
ret chosen.
].This is how Captain guarantees that a local always has a value when it is read: a name can only be read where every path to it has assigned it.
No shadowing. A block parameter may not reuse a name that is already in
scope (CAP-RESOLVE-007). A name means one thing throughout a body.
module Example::Scope.
export last lines: List[Text] -> Text [
line := "".
lines forEach: [ :line | ].
ret line.
].Parameters are locals too: a body may assign one (n := n + 1.), and the
new value must have the parameter's type.
Names in a module
Within a module, each declaration has one name in its namespace:
- procedures and effects are named by their verb and selectors together
(
total of:), sofind text:andfind path:are two different names; - structs, enums and aliases are type names;
- enum variants are value names, and must be unique across all the module's
enums. Two enums in one module cannot both have a
Red.
Declaring a name twice is CAP-RESOLVE-004. A type name may not reuse one
of the language's own types either (Text, List, Option and the rest).