Skip to content

Modules and imports

import "./thing.knip";
import "../lib/shapes.knip";
import "./thing.knip" as util;
import "./globals.knip" as *;
import perimeter, Cat from "./shapes.knip";

Paths are relative to the importing file. The .knip extension is optional. Imports go at the top of the file.

Form What you get
import "./lib.knip"; the public names, under lib.
import "./lib.knip" as geo; the public names, under geo.
import "./lib.knip" as *; every public name, with no prefix, plus the file’s sprites
import a, b from "./lib.knip"; just a and b, with no prefix; a name can be a sprite

An import brings its symbols in under a namespace — the file’s basename, or the alias:

import "./shapes.knip";
import "./shapes.knip" as geo;
private p: num = shapes.perimeter(4, 3);
private q: num = geo.perimeter(4, 3);

Only public symbols cross a file boundary:

lib.knip
public sides: num = 3;
private secret: num = 42;
public proc perimeter(side: num, count: num) -> num {
return side * count;
}
main.knip
import "./lib.knip";
private a: num = lib.sides; # ok
private b: num = lib.secret; # error: 'secret' is private

A module owns its variables, lists, dicts and structs, public and private. They live on the stage as lib.x, so names can’t collide across files. An importer can read and write the public ones, and the module’s own procedures can use all of them:

lib.knip
public SIDES: num = 3;
public counter: num = 0;
public shared: list<num> = [1, 2];
private calls: num = 0;
public proc bump() -> void {
counter += 1;
calls += 1;
}
main.knip
import "./lib.knip";
sprite Cat {
events.onFlag() {
lib.bump();
lib.counter = 10;
lib.shared.add(3);
looks.say(f"{lib.counter} {lib.shared[1]} {lib.SIDES}");
looks.say(f"{lib.calls}"); # error: Namespace 'lib' has no member 'calls'
}
}

A literal that is never written, like SIDES here, folds at the use site and adds no variable.

Every error in an imported file is reported at the import line, as path:line:col: message, so you can see where in that file it is.

Two limits:

  • A struct type does not cross. A public struct variable can be read and written through its fields (lib.pt.x), but lib.Pt(x = 1, y = 2) and a lib.Pt annotation are errors.
  • Struct names share one namespace across files. One name with two shapes is an error, so rename one.

import "./x.knip" as *; brings in every public name with no prefix. Any sprites the file declares (and their costumes and sounds) join the build too. This lets you split a big project into one file per sprite:

globals.knip
public score: num = 0;
private bonus: num = 1;
public proc addPoint() -> void { score += bonus; }
cat.knip
import "./globals.knip" as *;
sprite Cat {
events.onClick() { addPoint(); looks.say(Str(score)); }
}
main.knip
import "./globals.knip" as *;
import "./cat.knip" as *;
stage { events.onFlag() { score = 0; } }

A public variable imported this way is a plain stage variable named score, with no prefix. A private one still gets its file’s prefix (globals.bonus), so private names from different files don’t collide. Importing the same file as * from several files, as above, gives you one shared declaration.

These are errors:

  • a name from an as * import that clashes with one of your own
  • one public variable name in two files that are both imported as *
  • two sprites with the same name
  • a file that declares sprites, imported without as * or a name list

as * doesn’t re-export. main.knip gets score because it imports globals.knip itself, not through cat.knip.

import a, b from "./x.knip"; brings in only the names you list, unprefixed. A name can be a public variable, procedure or sprite. Everything else in the file keeps its prefix, so its variables can’t collide with yours:

pets.knip
public treats: num = 4;
public proc feed() -> void { treats -= 1; }
sprite Cat { events.onFlag() { looks.say(Str(treats)); } }
sprite Dog { }
import feed, Cat from "./pets.knip"; # Cat joins the build; Dog doesn't

A name the file doesn’t have as a public symbol (has no public 'nope') is an error, and so is a picked name that clashes with one of yours.

If a.knip imports b.knip, a file that imports a.knip cannot reach b through it:

a.knip
import "./b.knip";
public proc quad(n: num) -> num { return b.double(b.double(n)); }
main.knip
import "./a.knip";
a.quad(2); # ok — a uses b internally
a.b.double(2); # error — b is not re-exported

Each file names its own dependencies. There is no transitive namespace.

Each file is read and parsed exactly once, no matter how many files import it. Unresolvable paths and import cycles are reported against the file that did the importing, with a source span.

import("./levels.csv"), with parentheses and a .csv or .json path, is an expression that loads data rather than a module. See Loading a list or dict from a file.

Import resolution is pluggable. The CLI resolves against the filesystem; the VS Code extension prefers open documents so unsaved edits are picked up; an embedding can supply virtual files, which is how a browser playground would work.

If you are embedding the compiler, you pass a resolver — see Embedding the compiler.