← Back to AILP Home

use — Importing Modules

Syntax

use "path/to/module.npk".*;           // wildcard import (all pub symbols)
use "stdlib_file.npk".*;              // stdlib import (bare filename)
use "math.npk".{square, pi};          // selective import
use "utils.npk" as utils;             // namespace import

Path Resolution

Import Types

Wildcard Import (.*)

Imports all public (pub) declarations from the module directly into the current scope.

use "math.npk".*;
int32:val = square(5);

Selective Import (.{a, b})

Imports only the specified symbols from the module into the current scope.

use "math.npk".{square, pi};
int32:val = square(5);
// cube(5) // Error: undefined identifier

Namespace Import (as M)

Imports the module as a namespace. Symbols are accessed using the namespace prefix.

use "math.npk" as math;
int32:val = math.square(5);

Module Search Path

When resolving modules, the compiler searches directories in the following order:

  1. Current Directory: Relative to the importing source file (for file path imports).
  2. User Search Paths: Directories added via the -I <dir> command-line flags.
  3. Stdlib: The compiler's root directory and its stdlib/ subdirectory.
  4. System Directories: /usr/lib/aria (on Linux).
  5. Environment Variables: Directories listed in the NITPICK_PATH environment variable.

Diamond Import Behavior

Nitpick guarantees type identity across multiple paths. If module A and module B both import C, and your file imports both A and B, the types from C will match perfectly. There are no duplicate type definitions for the same logical imported module.

Error Handling Patterns

Example

// file: math_utils.npk
pub func:square = int32(int32:x) {
    pass (x * x);
};
pub const int32:pi = 3;

// file: main.npk
use "math_utils.npk" as math;

func:main = int32() {
    int32:val = raw math.square(math.pi);
    println(`&{val}`);    // 9
    exit 0;
};

func:failsafe = int32(tbb32:err) { exit 1; };

Notes

File Extension Conventions

Nitpick source files use the following accepted extension:

Extension Status When to use
.npk Canonical All new Nitpick code

use path: include the extension explicitly in the import path.

use "math_utils.npk".*;   // canonical

Related