LEVIATHAN v962456e · 962456eee1

leviathan-lang.com / docs / learn

Values and types

Static types, inference with var and let, methods on primitives, string interpolation, and const.

Declaring values

Leviathan is statically typed. You can spell a type explicitly, or let the compiler infer it with var (reassignable) or let (single-assignment). Inference never changes the fact that every binding has one static type.

var and let declare local bindings, so they live inside a function or other block. A top-level declaration always spells its type.

Explicit types and inference

int    count = 3;
string name  = "Ada";
bool   ready = true;
float  ratio = 0.75;
console.writeln("${count} ${name} ${ready} ${ratio}");

void inference() {
    var total = count + 10;     // inferred int, still reassignable
    let pi    = 3.14159;        // inferred float, fixed after this line
    total = total + 1;
    console.writeln(total);
    console.writeln(pi);
}
inference();
3 Ada true 0.750000
14
3.141590

Primitives are objects

Here is the first place the "one rule" shows up: primitives are objects. int, string, bool, float, and char are value types stored unboxed, but they carry real methods that dispatch through the same machinery as any class.

Methods on primitives

console.writeln((-7).abs());
console.writeln("Hello".toUpper());
console.writeln((42).toString() + "!");
console.writeln(3.7.floor().toInt());
7
HELLO
42!
3

String interpolation

Strings interpolate with ${...}, which desugars to concatenation through .toString():

Interpolation

int count = 3;
string who = "world";
console.writeln("Hello, ${who}! You have ${count + 1} messages.");
Hello, world! You have 4 messages.

Fixed bindings with const

When you want a binding fixed, reach for const. It is not a type: it scopes a slot's write window to its initialization, then leaves only the read view. Assigning to it afterwards is a compile error. (There is a construction-time sibling, readonly, for class fields, covered in Classes and objects.)

A const binding

const int maxRetries = 5;
// maxRetries = 6;   // compile error: cannot assign to const
console.writeln(maxRetries);
5