Skip to content
Sword

Packages

A package is a directory. Every .sword file in it shares one scope, so there are no headers, no forward declarations, and no ordering rules:

myproject/
main.sword
shapes/
shape.sword
square.sword

In main.sword:

import "shapes"
func main() int {
mut sq := shapes.NewSquare(6)
return int(sq.Area()) + 6
}

A name is visible outside its package when it starts with a capital letter. That applies to extern declarations too, so a package can wrap a C function without exposing it, and to the fields of a struct — a type can be public with its insides private:

package time
struct Duration {
ns i64 // nobody outside reads or writes this
}
func (d Duration) AsMillis() i64 { // this is the way in
return d.ns / 1000000
}

time.Duration{ns: 0} from another package is a compile error, and so is d.ns. Inside std/time both are ordinary code.

Imports are searched for next to the file being compiled first, then wherever the compiler keeps its standard library. Import cycles are a compile error.

Pointing the compiler at a single .sword file compiles that file alone — it will not quietly pull in its neighbours. Point it at a directory to compile the whole package.