leviathan-lang.com / docs / learn
Values and types
Static types, inference with var, 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. Inference never changes the fact that every binding has one static type: a var can be
reassigned only with values of the type its initializer gave it. A value that never changes spells
its type with const.
var works in a function or block, at the top level of a script and in a namespace. Its
initializer must have a type of its own, so var xs = []; is an error: write Array<int> xs = [];.
Explicit types and inference
use io::console;
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
const float pi = 3.14159; // fixed after this line
total = total + 1;
console.writeln(total);
console.writeln(pi);
}
inference();
3 Ada true 7.5e-1
14
3.14159e0
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
use io::console;
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
use io::console;
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
use io::console;
const int maxRetries = 5;
// maxRetries = 6; // compile error: cannot assign to const
console.writeln(maxRetries);
5