A tour of Kova
This page shows the main ideas of Kova in a few programs. Each section ends with a link to the chapter of the guide that explains it fully.
Values
Section titled “Values”fn main { let name: String := "Ada" var age: Int := 36 age += 1 print("{name} is {age}") print(age = 37)}Ada is 37truelet never changes, var may. := gives a value, = compares: there is no ==. Types are
written. Values and types →
Functions
Section titled “Functions”fn area(width: Float) by(height: Float) -> Float { return width * height}
fn greeting -> String { return "hello"}
fn main { print(area(3.0) by(4.0)) print(greeting.to_upper)}12.0HELLOA name can be cut into parts, area(…) by(…), so a call reads like a sentence. A function without
parameters has no (). Functions →
Structs and enums
Section titled “Structs and enums”struct Dog { name: String age: Int
fn birthday(var self) { self.age += 1 }}
enum Size { Small Large(kilos: Int)}
fn describe(s: Size) -> String { return match s { .Small => "small" .Large(k) => "large, {k} kg" }}
fn main { var rex := Dog { name := "Rex", age := 3 } rex.birthday print(rex) print(describe(Size.Large(30)))}Dog { name := "Rex", age := 4 }large, 30 kgA match must handle every case of an enum: forgetting one is an error.
Structs → Enums →
Ownership
Section titled “Ownership”fn add_one(l: var List<Int>) { l.add(1)}
fn main { var l: List<Int> := [5] add_one(var l) print(l) let m: List<Int> := l print(m)}[5, 1][5, 1]Memory is freed without a garbage collector: each value has one owner. A call that changes a
value says var at the call; let m := l moves the list to m, and l can no longer be used.
Ownership →
Missing values and errors
Section titled “Missing values and errors”fn half(n: Int) -> Int or String { if n % 2 != 0 { return err("{n} is odd") } return ok(n / 2)}
fn main { match "42".to_int { none => print("not a number") n => print(half(n)) } print(half(7))}ok(21)err("7 is odd")Int? may be none; T or E is a result or an error. The compiler makes sure both are looked at.
Missing values and errors →
Collections and blocks
Section titled “Collections and blocks”fn main { let scores: List<Int> := [12, 5, 18, 7] print(scores.filter { x: Int => x > 10 }) print(scores.sorted) let ages: Map<String, Int> := ["Ana" := 31, "Bo" := 25] print(ages["Bo"])}[12, 18][5, 7, 12, 18]25Lists, Maps and Sets → Blocks →
Contracts and tests
Section titled “Contracts and tests”fn divide(a: Int, b: Int) -> Int require b != 0 else "you cannot divide by zero"{ return a / b}
test "divide rounds down" { check divide(7, 2) = 3}
fn main { print(divide(9, 2))}4A function says what it needs; kova test runs the tests.
Contracts and tests →
And the library
Section titled “And the library”Files, the web and a web server, JSON and CSV, dates, patterns, encryption, native windows and calls to C come with Kova: the library.