Import statements allow you to use code from other modules in your Conjure programs. Imports must appear at the top of your source file, before any declarations.
Basic Imports
Use the import keyword followed by the module path in quotes. The last component of the path becomes the local namespace.
import "conjure/io"
// Now we can use io.print, io.println, etc.
io.println("Hello, world!")An important thing to note is that imported symbols are not exported by the importing module. This means that the io namespace will not be available to modules that import this module unless they also explicitly import conjure/io.
Aliased Imports
Use as to give the imported module a different local name. This is useful when module names conflict or when you want a shorter name.
import "conjure/memory" as mem
var buffer = mem.alloc(1024)
defer mem.free(buffer)Named Imports
Import specific symbols from a module by listing them in parentheses. This brings only the named symbols into scope without requiring a namespace prefix.
import "conjure/memory" as (alloc, free)
// Use the imported symbols directly
var buffer = alloc(1024)
defer free(buffer)You can import multiple symbols:
import "conjure/math" as (sin, cos, PI)
var x = cos(PI / 4.0)
var y = sin(PI / 4.0)Wildcard Imports
Use * to import all public symbols from a module directly into the current namespace.
import "conjure/math" as *
// All math symbols are now available without a prefix
var angle = PI / 2.0
var result = sin(angle) + cos(angle)Note: Use wildcard imports sparingly, as they can make it unclear where symbols originate and may cause naming conflicts.
Relative Imports
Use ./ to import modules relative to the current file’s directory.
import "./utils"
import "./game/player"Standard Library Modules
Conjure provides several built-in modules in the standard library:
import "conjure/io" // Input/output operations
import "conjure/memory" // Memory allocation and management
import "conjure/math" // Mathematical functions
import "conjure/string" // String manipulation utilitiesMultiple Imports
Import multiple modules by using separate import statements.
import "conjure/io" as io
import "conjure/math" as math
import "conjure/memory" as memUsing Imported Symbols
Once imported, access the module’s public symbols using dot notation.
import "conjure/io" as io
import "conjure/math" as math
func main() {
var angle f32 = math.PI / 4.0
var result = math.sin(angle)
io.println("sin(PI/4) = #{result}")
}Module Visibility
Symbols in a module are public by default and can be accessed from importing code. Symbols prefixed with an underscore (_) are private to the module.
// In mymodule.cjr
var publicVar = 42 // accessible from importers
var _privateVar = 99 // only accessible within this module
func publicFunc() { } // accessible from importers
func _privateFunc() { } // only accessible within this moduleImport Order
Import statements must appear:
- After any comments at the top of the file
- Before any variable, constant, function, or type declarations
// File header comments are allowed before imports
import "conjure/io" as io
// All imports must come before declarations
var globalCounter = 0
func main() {
io.println("Counter: #{globalCounter}")
}