Modules & Privacy
Conduit features an explicit, file-based module system designed for encapsulation, predictable encapsulation boundaries, and maintainable project architectures.
File-Based Modules & Privacy by Default
In Conduit, each .cdt source file constitutes an independent module.
By default, all declarations (types, errors, and functions) are private to the file in which they are declared. They cannot be referenced, called, or constructed by other modules unless explicitly marked for export.
Explicit Exports (export)
To expose an item to external modules, prefix its declaration with the export keyword:
// users.cdt
export struct User {
id: Int
name: String
}
export error UserNotFound {
id: Int
}
export func find_user(id: Int) -> User | UserNotFound {
guard id > 0 else {
return UserNotFound(id: id)
}
return User(id: id, name: "Alice")
}Any item without export remains private:
// Private helper function: unreachable outside users.cdt
func format_name(n: String) -> String {
return "User: " + n
}Prevention of Private Type Leakage
To prevent encapsulation leaks, the type checker enforces that exported functions and types cannot leak private types in their public signatures:
struct SecretConfig {
key: String
}
// Error! Exported function leaks private type SecretConfig:
export func get_config() -> SecretConfig {
return SecretConfig(key: "secret")
}Diagnostic:
Type error: Exported function 'get_config' exposes private type 'SecretConfig' in return typeImporting Modules (import)
Modules are imported using the import statement at the top of a file.
Local Relative Imports
Local modules support TypeScript-style named imports, aliased imports, and namespace imports:
import { User, find_user } from "./users"
import * as users from "./users"Items imported via namespace alias are accessed with qualified syntax (users.User, users.find_user(42)).
Standard Library Imports
Standard libraries are imported from the cdt.* namespace:
import { now, elapsed } from "cdt.time"
import * as time from "cdt.time"Canonical Type Identity & Import Aliases
Every type and error in Conduit is uniquely identified by its canonical declaring module path together with its declaration name. Import aliases only affect how a symbol is spelled locally:
- Importing the same module under two aliases (
import * as u1 from "./users"andimport * as u2 from "./users") yields the exact same canonical type, sou1.Userandu2.Userare interchangeable across assignments, function calls, andswitchpatterns. - Same-named declarations in distinct modules (such as
a.Userandb.User) remain strictly separate, incompatible types at both compile time and runtime. Struct schemas used bycdt.validation.checkalso follow canonical identity and never collide across modules.
Cycle Detection
Conduit statically analyzes the module dependency graph before type checking. If cyclic dependencies exist (e.g., A -> B -> C -> A), the compiler halts immediately with a clear dependency trace:
Loader error: Cyclic import detected: ./a -> ./b -> ./c -> ./aRunnable Example
Module Definition (modules_user.cdt)
export struct User {
name: String
}
export error MissingUser {
id: Int
}
export func load_user(id: Int) -> User | MissingUser {
guard id > 0 else {
return MissingUser(id: id)
}
return User(name: "Alice")
}Module Consumer (modules_main.cdt)
import { load_user, User, MissingUser } from "./modules_user"
func main() -> Void {
switch load_user(1) {
case User(u): print("Hello, {u.name}")
case MissingUser(_): print("Missing user")
}
switch load_user(0) {
case User(_): print("Found")
case MissingUser(_): print("Successfully caught missing user error")
}
}Running the Multi-Module Project
conduit run modules_main.cdtOutput:
Hello, Alice
Successfully caught missing user error