Velo by Example: Generics

Velo supports generic classes and functions with type parameters in square brackets. Generics are a compile-time feature with no runtime overhead.

A generic class with a type parameter T.

class Box[T](T value) {
    func get() T {
        value;
    };
    func set(T newValue) void {
        value = newValue;
    };
};

Box[int] intBox = new Box[int](42);
Box[str] strBox = new Box[str]("hello");

Multiple type parameters are separated by commas.

class Pair[T, U](T first, U second) {
    func getFirst() T { first; };
    func getSecond() U { second; };
};

Pair[int, str] p = new Pair[int, str](42, "hello");
int n = p.getFirst();     # 42
str s = p.getSecond();    # "hello"

Generic functions infer type arguments automatically from call arguments — no explicit type needed.

func identity[T](T value) T {
    value;
};

int x = identity(42);        # T = int
str s = identity("hello");   # T = str

A generic container (stack) using arrays.

class Stack[T]() {
    array[T] items = new array[T](0);
    int size = 0;

    func push(T item) void {
        items = items.plus(item);
        size = size + 1;
    };

    func peek() T {
        items[size - 1];
    };
};

Stack[str] stack = new Stack[str]();
stack.push("a");
stack.push("b");
str top = stack.peek();  # "b"