← Back to the index

8. Modules and Imports

In this chapter

Module Structure

A vv file typically represents a module. Code is executed sequentially from top to bottom.

Importing Modules

To use code from another module, you use the import name from "path" syntax.

import math from "./math_utils.vv"
let result = math.add(10, 5)

Exporting with pub

By default, all declarations in a module are private to that module. To make them accessible to other modules that import it, you must use the pub keyword. Only let, fun, and extern declarations can be exported.

// math_utils.vv
let secret_key = "12345" // Private to this module
pub let PI = 3.14159     // Publicly accessible

fun calculate_internal() // Private function
    // ...
end

pub fun calculate_area(radius) // Public function
    return PI * radius * radius
end

Exporting Variables

Prefix a let declaration with pub to export a variable or constant.

pub let PI = 3.14159

Exporting Functions

Prefix a fun declaration with pub to export a named function.

pub fun calculate_area(radius)
    return PI * radius * radius
end

Exporting External Declarations

You can also export external (native) declarations using pub extern. This allows other modules to use the native functions or variables provided by the host environment.

pub extern "native" fun cos(x)

System Imports

Standard library modules can be imported by specifying the std/ prefix followed by the module name.

import console from "std/console.vv"
console.print("Hello, system!")