Your first project

This page shows how to create and run a program, add the standard library, and then make a library with tests.

Before you start, install zane and a compiler (Install Zane).

Create and run a program

zane init hello --yes
cd hello
zane run

zane init asks a few questions (the package name, library or application, whether to start a Git repository); --yes accepts every default. It creates:

hello/
  zane.coda          the manifest: compiler version and dependencies
  zane-lock.coda     the lock file: the exact commits of the compiler and dependencies
  bin/hello/main.zn  the program
  .gitignore         lists out/, where builds go

zane run builds the program and runs it, printing hello world. The generated main.zn uses only the compiler’s built-in names, such as @primitives$String, because a new project has no dependencies yet (Intrinsics and concepts explains the @ names).

Add the standard library

The types you normally write with, such as Int, String, List, and even if, come from the core package of std. Add it:

zane add https://github.com/zane-lang/std --from-source

std publishes no prebuilt objects, so --from-source has zane compile it in your project. Now replace bin/hello/main.zn with:

package hello;

import core$;

Unit main() {
	console Console(@program$console);
	name String = String("Zane");
	console!print("Hello, \%name!");

	total Int = Int(0);
	i Int = Int(1);
	i!to(Int(10)) {
		total = total + i;
	}
	console!print("1 + 2 + ... + 10 = " + String(total));
	return Unit();
}
Hello, Zane!
1 + 2 + ... + 10 = 55

and run it again with zane run. The first line of every source file names its package: here hello, the directory under bin/. import core$; makes the names of core usable without a prefix. main is where the program starts. Console(@program$console) creates a Console from the program’s console. The language gives the console to the program as @program$console instead of providing a global, so code that you never give the console to cannot print (Effects). "\%name" inserts the value of name into the string.

The language tour explains each of these in turn.

The commands you use most

Command Effect
zane check Type-checks the project without building it: the fast loop while editing
zane run Builds a program and runs it; arguments after -- go to the program
zane build Builds every program in bin/, optimized, into out/host/
zane test Builds and runs every test package in test/
zane clean Deletes out/

Each command works from any directory inside the project. The zane command lists every command.

A library and its tests

A library keeps its code in lib/ and its tests in test/:

zane init shapes --lib --yes
cd shapes
zane add https://github.com/zane-lang/std --from-source

zane init --lib creates lib/shapes/shapes.zn, the library package shapes, and test/shapes/main.zn, a test package for the library. Put this in lib/shapes/shapes.zn:

package shapes;

import core$;

/// A rectangle, by its width and height.
type Rect = struct {
	width Int;
	height Int;
}
Rect(width Int, height Int) => init{ width; height; }

Int area(this Rect) => this.width * this.height;

/// Whether the rectangle is a square.
Bool isSquare(this Rect) => this.width == this.height;

Every declaration is public unless its name starts with _. A test package uses the library as any other user does, by importing it. Put this in test/shapes/main.zn; the first line of a test package is always package test;:

package test;

import core$;
import shapes$;

/// Prints each check's result, and counts the checks that fail.
type Checks = #struct {
	_console Console;
	_failed Int;
}
Checks() => init{
	_console = Console(@program$console);
	_failed = Int(0);
}

Unit check(this Checks, name String, ok Bool) mut {
	passed Bool = if(ok) {
		this._console!print("ok   " + name);
	}
	passed:else() {
		this._failed = this._failed + 1;
		this._console!print("FAIL " + name);
	}
	return Unit();
}

/// Aborts with a message when any check failed.
Unit?@primitives$String finish(this Checks) {
	if(this._failed > 0) {
		abort @primitives$String(String(this._failed) + " of the checks failed");
	}
	return Unit();
}

Unit?@primitives$String main() {
	checks Checks();
	r Rect(Int(3), Int(4));
	checks!check("area", r:area() == 12);
	checks!check("not square", ~r:isSquare());
	checks:finish() ? report {
		abort report;
	}
	return Unit();
}

Then run the tests:

$ zane test
Testing test/shapes/
ok   area
ok   not square
1 test package passed.

When a check fails, finish aborts, and main passes the abort on: the test package writes 1 of the checks failed to stderr and exits with 1, and zane test reports the package as failed.

A project can hold library packages, programs and tests together: each directory under bin/ is a program. Testing covers test packages in more depth, and Projects describes the files zane keeps in a project.