Language guide · 1 of 12
Packages & Imports
Every standalone Evie file or module begins with a package declaration, optionally importing other packages.
package main imports("io", "time")
fn main() {
io.println("Hello from Evie!")
}Open in playground- Standard library packages (
io,fs,math,time,json, see section 12) can be imported directly, as can packages the host registers and.evfiles by relative path (imports("./util")). - Every top-level function, class and binding of a package is visible to importers under the package name (
io.println,time.wait). There is no private marker; keep internal helpers in a package that is not imported. An importer always sees a package'svarbindings as they are now and can assign them (util.count = 0); assigning one of its constants is a compile error. - Functions, classes and constants whose initializer is itself constant (
SECONDS := 60,MILLIS := SECONDS * 1000) are hoisted, so they can be used before their declaration. Every other top-level binding is initialized in source order when the package loads; its initializer can call functions and build any value, and a binding read before its line has run isnil:
package main imports("io")
config := { debug: false, retries: 3 } // any expression, run at load time
limits := computeLimits(config.retries)
fn computeLimits(n) => [n, n * 2]
fn main() {
io.println(config.debug, limits)
}Open in playgroundOrganizing a program
A package is one file or one folder; a program is the packages its entry point imports.
shop/
main.ev package main imports("./cart", "./util/money")
checkout.ev package main (more of main)
cart/
cart.ev package cart
items.ev package cart (more of cart)
util/
money.ev package moneyOpen in playground- A folder is one package. Every
.evfile in it declares the same name, and the files are compiled together, so any file can use what another declares. - A project's main package is every top-level file that declares
package main.evie shop/(oreng.RunProject) runs it; the other top-level files are packages imported by path.evie file.ev(oreng.RunFile) runs that one file. - Imports name a path or a package.
"./cart"and"../lib/text"are files or folders relative to the importing file, tried ascart.evand thencart/. A bare name such as"io"is a package the host or the standard library provides. A localmath.evis imported as"./math". - An import is bound by its alias or declared package name:
imports("./util/money")binds asmoney, andimports("./util/money" as m)binds asm(asis contextual insideimports(...)only). - Imports belong to the whole package, so a package imports a module once, in any of its files.
- Modules are identified by their canonical path, not only by short name. Two different folders can both declare
package util(or a local./math.evcan declarepackage mathalongside stdlib"math"). If a single package imports both, give one an alias:imports("math", "./math" as localMath). A folder whose files disagree on their package name, or importing amainfile, is an error. - Errors inside an imported package are reported in that package's file, with a note at the import.
- An engine runs one program and loads each module once; create a new engine for the next program (or parse once into an
*evie.Bundleand run it across multiple engines).