Docs/Types

Interfaces

Accept different types through one set of methods.

On this page

An interface lists methods that different classes and structs provide. A function that takes the interface accepts any type that implements it.

Declare and implement#

Valk
interface Shape {
    fn area() float;
    get name: String;
}

class Square is Shape {
    side: float
    fn area() float { return this.side * this.side }
    get name: String { return "square" }
}

class Circle is Shape {
    radius: float
    fn area() float { return 3.14 * this.radius * this.radius }
    get name: String { return "circle" }
}

fn report(shape: Shape) {
    println(shape.name + ": " + shape.area())
}

fn main() {
    report(Square { side: 2.0 })   // square: 4
    report(Circle { radius: 1.0 }) // circle: 3.14
}
  • Interface methods end in ;. Getters are declared with get name: Type;.
  • A class or struct lists its interfaces after is, separated by commas: class User is Named, Printable.
  • Each method must match the declared arguments, return type and error type.

Default methods#

An interface method with a body is used by classes that do not define their own:

Valk
interface Greeter {
    get name: String;
    fn greet() String {
        return "Hello, " + this.name
    }
}

class Person is Greeter {
    first: String
    get name: String { return this.first }
}

class Robot is Greeter {
    get name: String { return "R2" }
    fn greet() String { return "Beep" }
}

fn main() {
    let list = Array[Greeter]{ Person { first: "Ada" }, Robot {} }
    each list as item : println(item.greet())
    // Hello, Ada
    // Beep
}

Errors in interface methods#

An interface method can declare an error. A class method that never fails may still implement it; callers through the interface handle the error, direct callers do not:

Valk
union LoadError { missing }

interface Source {
    fn load() String !LoadError;
}

class Fixed is Source {
    text: String
    fn load() String { return this.text }
}

fn main() {
    let fixed = Fixed { text: "data" }
    println(fixed.load())        // no handler needed
    let source: Source = fixed
    println(source.load() !? "") // data
}

Check the concrete class#

is_a tests which class an interface value holds. match also gives the object as that class. A default case is required, because other classes may implement the interface:

Valk
interface Animal {
    fn sound() String;
}

class Dog is Animal {
    fn sound() String { return "woof" }
    fn fetch() String { return "fetching" }
}

class Cat is Animal {
    fn sound() String { return "meow" }
}

fn main() {
    let pet: Animal = Dog {}
    if pet is_a Dog : println("a dog")

    match pet {
        Dog as dog => println(dog.fetch())
        default => println(pet.sound())
    }
}

Structs implementing interfaces#

Structs implement interfaces the same way with is, and support two dispatch styles:

  • Static dispatch (zero allocation): Use a generic constraint [T is Stepper] to keep the concrete struct type with no heap allocation.
  • Dynamic dispatch (boxed): Pass or store a struct directly as the interface type (such as let s: Stepper = ... or Array[Stepper]). Valk boxes the struct on the heap so structs and classes can be mixed together, and mutations through interface methods persist in that boxed value.
Valk
interface Stepper {
    fn step() int;
}

struct Counter is Stepper {
    count: int (0)

    fn step() int {
        this.count++
        return this.count
    }
}

class FixedStep is Stepper {
    value: int
    fn step() int { return this.value }
}

// Static dispatch: specialized for T, no allocation
fn advance_twice[T is Stepper](s: T) int {
    s.step()
    return s.step()
}

fn main() {
    let counter = Counter { count: 10 }
    println(advance_twice(counter)) // 12 (static dispatch on a copy)

    // Dynamic dispatch: boxed struct keeps its state across calls
    let boxed: Stepper = Counter {}
    println(boxed.step())           // 1
    println(boxed.step())           // 2

    // Mix structs and classes in one interface collection
    let list = Array[Stepper]{ Counter { count: 5 }, FixedStep { value: 99 } }
    each list as item : println(item.step()) // 6, then 99
}

is_a and match (including struct field destructuring such as Counter { count }) work on boxed structs just like classes.

Interface or generics#

Use an interface when one list or parameter must accept different types. Use generics (with constraints like [T is Stepper]) when you want zero-allocation static dispatch or to keep the caller's concrete type, and a tagged union when the set of types is closed and known.