Skip to main content

Namespaces and imports

Every file starts with its namespace, which says where the file's declarations live. To use a name from another namespace, a file imports it, or writes it out in full.

A file's namespace​

namespace is the first line of every file. Only comments and blank lines may come before it.

namespace robots.arm

let reach = 30

reach is now robots.arm.reach. A namespace is a dotted name, and its parts say how it's grouped: the Raspberry Pi library's is grandtheorem.rpi, the Build HAT's is grandtheorem.buildhat, and the standard library's are nylon.time and nylon.math.

Doesn't compile

A file without a namespace is refused, and the build says to write namespace first:

let reach = 30

Importing​

There are two kinds of import: one for a single name, and one for a whole namespace.

namespace robot

import nylon.math.PI
import namespace nylon.time

entrypoint main() {
let turn = 2.0 * PI
sleep(500.milliseconds())
}
  • import a.b.Name brings in one declaration: a class, a trait, a function or a value. Here, PI.
  • import namespace a.b brings in everything the namespace offers, so sleep and now from nylon.time can be written on their own.

An import names exactly one of these. A plain import of a namespace is refused:

Doesn't compile

nylon.time is a namespace, so the build says to write import namespace nylon.time, or to name one of its declarations:

namespace robot

import nylon.time

entrypoint main() {
sleep(500.milliseconds())
}

Renaming with as​

as gives an import another name, for this file only.

namespace robot

import nylon.math.PI as pi
import namespace nylon.time as clock

entrypoint main() {
let turn = 2.0 * pi
clock.sleep(500.milliseconds())
}

A namespace imported with as is reached through its new name alone: clock.sleep, never a bare sleep. A renamed declaration's old name is gone too, so in this file PI means nothing.

The same name twice​

  • The same declaration imported again is harmless, and IntelliJ warns on the second import: "'PI' is already imported on line 3". Alt+Enter removes it.
  • Two declarations under one name is an error on the second import, which says to import it under another name with as.

No import needed​

Every namespace can be named from its root, with no import at all:

namespace robot

entrypoint main() {
let start = nylon.time.now()
nylon.time.sleep(5.milliseconds())
let took = nylon.time.now() - start
}

A file's own namespace works the same way, so robots.arm.reach reaches reach inside namespace robots.arm. An import is the short way to write the same thing.

A name is looked for in the file first, then in its imports, and the root namespaces come last. So a local named nylon hides the namespace nylon in that function.

When a name isn't found​

A name that no import brought in is refused. When one namespace declares it, the message says which import to write:

Doesn't compile

sleep isn't imported, so the build says "'sleep' is nylon.time's, so write import nylon.time.sleep":

namespace robot

entrypoint main() {
sleep(500.milliseconds())
}

A name that several namespaces declare is just unknown, since the build can't tell which one you meant. Import the one you want.

Overloaded functions​

A function with several overloads, such as nylon.math.abs, can't be imported by name in V0. Import its namespace, or call it from its root:

namespace robot

import namespace nylon.math

let distance = abs(-3)
let other = nylon.math.abs(-4)

Not in V0​

import proxy and import instance are refused by name: "'import proxy' isn't in V0 yet: write 'import a.b' for a type".

Imports from a library​

A library's namespaces are imported like any other, once the build file names the library. Libraries shows how to add one, and which namespaces each library has.