export
export makes a top-level declaration of a library visible to other modules. A declaration without it is private to its file.
Details
exportgoes before a top-levelvar,let,func,type, orinterface, and nowhere else.- Only a library exports. A file with
exportcannot have a top-level statement, since a script cannot be imported. - An exported function states its return type. A function exported on a type needs the type exported too.
- An interface that an export names must be exported too: in a function's signature, an interface's functions, a type's fields or bounds, or a variable's type. The importer must be able to name what it is asked to satisfy. Any other private type may appear there.
- An imported
varreads the exporting module's current value, but only that module can assign it. - Privacy applies to names, not to data. A test file sees every declaration of the modules it imports, exported or not.
Basic examples
A library marks what other files may use:
export type Point = { x: float64, y: float64 }
export func Point.distance(a: Point, b: Point): float64 {
Math.sqrt(square(a.x - b.x) + square(a.y - b.y))
}
func square(n: float64): float64 {
n * n
}A script imports the exported names. The functions exported on Point come with it:
import "./geometry.ant" { Point }
let p = Point{ x: 3.0, y: 4.0 }
println(p.distance(Point{ x: 0.0, y: 0.0 }))5.0square has no export, so no other module can import it:
import "./geometry.ant" { square } // error: square is not exported by ./geometry.antScope
Variables and constants
export var and export let export a module's state. An imported var is live: each read sees the module's current value.
export let LIMIT = 3
export var count = 0
export func bump(): void {
if count >= LIMIT {
return
}
count += 1
}import "./counter.ant" { LIMIT, count, bump }
println(count)
for _ in Iter.range(0, 5) {
bump()
}
println("${count} of ${LIMIT}")0
3 of 3Namespace imports
A module imported under one name is a namespace of its exports, read with .:
import "./counter.ant" Counter
Counter.bump()
println(Counter.count)1Interfaces
export interface exports an interface. A type satisfies it outside its module only through exported functions: here Circle.area is private, so another module cannot use a Circle as a Shape.
export interface Shape {
area(s: Self): float64
}
export type Square = { side: float64 }
export func Square.area(s: Square): float64 {
s.side * s.side
}
export type Circle = { radius: float64 }
func Circle.area(c: Circle): float64 {
Math.PI * c.radius * c.radius
}
export func total(shapes: Array[Shape]): float64 {
(shapes | Array.map({ x.area() }) | Array.sum()) ?? 0.0
}import "./shapes.ant" { Shape, Square, total }
let squares: Array[Shape] = [Square{ side: 2.0 }, Square{ side: 3.0 }]
println(total(squares))13.0import "./shapes.ant" { Shape, Circle }
let s: Shape = Circle{ radius: 1.0 }
// error: cannot use Circle where Shape is expected: Circle.area is not exportedPrivate types
A field of an exported type may have a private type that is not an interface. The importer reads the field but cannot name its type:
type Token = { value: string }
export type Session = { token: Token }
export func Session.start(): Session {
Session{ token: Token{ value: "abc" } }
}import "./session.ant" { Session }
let s = Session.start()
println(s.token.value)abcimport "./session.ant" { Token } // error: Token is not exported by ./session.antTest files
A test file imports private declarations as it imports exports, so it can test them directly:
import "./session.ant" { Session, Token }
test "start issues a fresh token" {
assert(Session.start().token == Token{ value: "abc" })
}Possible issues
A script cannot export. One top-level statement makes the file a script:
export let answer = 42
println(answer)
// error: a module with exports cannot have top-level statementsAn exported function must state its return type, so the module's interface reads without its bodies:
export func double(n: int) { n * 2 }
// error: the exported function double must state its return typeA function exported on a private type is a compile error:
type Point = { x: int, y: int }
export func Point.origin(): Point { Point{ x: 0, y: 0 } }
// error: Point.origin is exported but Point is notSo is an exported function whose signature names a private interface, since the importer could not check a call without it:
interface Shape {
area(s: Self): float64
}
export func total(shapes: Array[Shape]): float64 {
// error: total is exported but names the private interface Shape in its signature
shapes.map({ x.area() }).sum() ?? 0.0
}Imports are read-only. Only the module that declares an exported var assigns it, so change it through an exported function such as bump:
import "./counter.ant" Counter
import "./counter.ant" { count }
count = 5 // error: cannot assign to the imported variable count
Counter.count = 5 // error: cannot assign to Counter.count: imports are read-onlyexport works only at the top level:
func answer(): int {
export let x = 42 // error: 'export' is only allowed at the top level
x
}There is no re-export. A module that imports bump does not pass it on to its own importers, and export takes a declaration, not a name:
import "./counter.ant" { bump }
export func twice(): void {
bump()
bump()
}import "./twice.ant" { twice, bump } // error: bump is not exported by ./twice.antimport "./counter.ant" { bump }
export bump
// error: expected var, let, func, type, or interface after export, got identifier "bump"See also
import, func, type, interface, let, var, test, Modules, ant test
