Language guide · 7 of 12

Classes & Object-Oriented Programming

Evie supports object-oriented programming with fixed-shape classes, declared body fields (mutable or immutable), encapsulated methods, explicit this, first-class bound methods, and single inheritance.

Class Definition & Instantiation

Classes are declared with class Name(field1, field2, ...) { ... }. The body braces { ... } are mandatory (even if empty). Constructor arguments passed during instantiation must match the number of constructor fields declared in parentheses.

class Person(name, age) {
    // Body fields: immutable by default (:=) or mutable with var
    kind := "human"
    var email := ""

    // Methods are declared directly inside the class body
    fn greet(prefix) {
        return prefix + ", I am " + this.name
    }

    // Arrow syntax works for short methods
    fn isAdult() => this.age >= 18

    // Mutating instance fields via this
    fn haveBirthday() {
        this.age = this.age + 1
    }
}

// Instantiation:
p := Person("John", 22)
io.println(p.name) // "John"
io.println(p.age)  // 22
io.println(p.kind) // "human"

// Method calls:
io.println(p.greet("Hello")) // "Hello, I am John"
io.println(p.isAdult())      // true

p.haveBirthday()
io.println(p.age)            // 23
Open in playground

Methods, this, & Bound Methods

greeter := Person("Alice", 30)
sayHi := greeter.greet // extracts bound method

io.println(sayHi("Hi")) // "Hi, I am Alice"
Open in playground

Fixed Shape & Body Fields

Classes have a fixed field layout known at compile time (constructor fields + body fields + inherited body fields). Assigning to an undeclared field is a runtime error; use an object ({ ... }) when you need arbitrary dynamic keys.

class User(first, last) {
    full := this.first + " " + this.last // immutable, computed per instance
    var email := ""                      // mutable with constant default
    var tags := []                       // fresh array per instance
}

u := User("Ada", "Lovelace")
u.email = "ada@example.com"
u.tags.push("math")
// u.full = "other"  // Error: cannot assign to constant field 'full' on class 'User'
// u.phone = "555"   // Error: class 'User' has no field 'phone'
Open in playground

Classical Single Inheritance

Classes support single inheritance using : followed by the parent class name (class Dog(...) : Animal { ... }). Subclasses inherit parent methods, can override methods, and can add their own subclass-specific methods:

class Animal(name) {
    fn speak() {
        return this.name + " makes a generic animal sound"
    }

    fn describe() {
        return "Animal named " + this.name
    }
}

// Dog inherits from Animal
class Dog(name, breed) : Animal {
    // Method override
    fn speak() {
        return this.name + " barks: Woof! Woof!"
    }

    // Subclass-specific method
    fn fetch(item) {
        return this.name + " fetched the " + item
    }
}

// Cat inherits from Animal
class Cat(name) : Animal {
    // Method override
    fn speak() {
        return this.name + " purrs: Meow"
    }
}
Open in playground

Usage:

a := Animal("Creature")
d := Dog("Rex", "German Shepherd")
c := Cat("Luna")

// Polymorphic method calls:
io.println(a.speak()) // "Creature makes a generic animal sound"
io.println(d.speak()) // "Rex barks: Woof! Woof!"
io.println(c.speak()) // "Luna purrs: Meow"

// Inherited methods from Animal:
io.println(d.describe()) // "Animal named Rex"
io.println(c.describe()) // "Animal named Luna"

// Subclass-specific method:
io.println(d.fetch("ball")) // "Rex fetched the ball"
Open in playground

Calling Parent Methods (super)

Inside a subclass method, use super.method(...) to call the parent class's implementation on the current this receiver, or super.method to extract a bound method:

class LoudDog(name, breed) : Dog {
    fn speak() {
        return super.speak() + "!!!"
    }
}

io.println(LoudDog("Max", "Husky").speak()) // "Max barks: Woof! Woof!!!!"
Open in playground

Polymorphic Type Checking (is Operator)

Subclasses satisfy both their own class type and all ancestor class types:

d := Dog("Rex", "German Shepherd")
c := Cat("Luna")

d is Dog     // true
d is Animal  // true (inherited)
c is Animal  // true
!(c is Dog)  // true
Open in playground

Classes and Concurrency

Class instances need no locking. Fibers of an instance never run at the same time, so a method like fn inc() { this.count = this.count + 1 } called from several tasks always counts every call.

Edit this page on GitHub