VOO

VOO (Vanilla Object Orientation) is a lightweight OO framework that composes classes from Tcl's native data structures — lists and dictionaries — rather than introducing additional framework infrastructure. VOO objects are plain Tcl lists with automatic memory management through copy-on-write semantics, eliminating the destructor burden inherent in TclOO and Itcl.

Benchmarks on Tcl 8.6.13 and Tcl 9.0 show VOO achieves 7–18× faster object creation and 4–6× superior memory efficiency compared to TclOO. A companion C++ migration path further improves field-access speed and memory usage while preserving an identical Tcl call-site API.

GitHub Repository: https://github.com/aaraujo-main/voo

Official Docs / Full API:https://github.com/aaraujo-main/voo/blob/main/README.md

Quickstart

package require voo

source voo.tcl

voo::class Point {
    public {
        double_t x 0.0
        double_t y 0.0
    }

    method distance {} {
        set dx [get.x $this]
        set dy [get.y $this]
        return [expr {sqrt($dx*$dx + $dy*$dy)}]
    }
}

set p [Point::new 3.0 4.0]
puts [Point::get.x $p]       ;# 3.0
puts [Point::distance $p]    ;# 5.0

Point::set.x p 6.0
puts [Point::get.x $p]       ;# 6.0

Quick Reference

Class Declaration

voo::class ClassName ?-virtual? ?-extends ParentClass? {
    public {
        type_t fieldName defaultValue
        type_t -static staticFieldName defaultValue
    }
    private {
        type_t fieldName defaultValue
    }
    method name {args} { body }
    constructor {args} { body }
    importMethods {parentMethod1 parentMethod2}
}

Field Types

Type Description Default Example
double_t Floating-point 0.0 double_t x 0.0
int_t Integer 0 int_t count 0
string_t String "" string_t name ""
bool_t Boolean 0 bool_t active 1
list_t List {} list_t items [list]
dict_t Dictionary {} dict_t data [dict create]
obj_t Any object {} obj_t nested {}

Constructors (auto-generated)

set obj [Class::new arg1 arg2 ...]        ;# Positional
set obj [Class::new()]                     ;# Default values
set obj [Class::new.args -field1 val ...]  ;# Named arguments

Accessors (auto-generated per field)

set val [Class::get.field $obj]                       ;# Getter
Class::set.field obj value                             ;# Setter
Class::update.field obj temp { modify $temp }          ;# Updater (COW-safe)

set val [Class::class.get.staticField]                 ;# Static getter
Class::class.set.staticField value                     ;# Static setter

Method Modifiers

Modifier Description
(none) $this passed by value
-static No this argument
-upvar this passed by reference
-update {fields} Named fields detached during body (COW-safe)
-override Validates parent method exists
-virtual Enables polymorphic dispatch (supports -upvar and -update)

Inheritance

voo::class Child -extends Parent {
    # Inherits parent fields and accessors
    method parentMethod -override {} { ... }
    importMethods {parentMethod1 parentMethod2}
}

Virtual Polymorphism

voo::class Shape -virtual {
    method area -virtual {} { return 0.0 }
}
voo::class Circle -extends Shape {
    public { double_t radius 1.0 }
    method area -override {} {
        return [expr {3.14159 * [get.radius $this] ** 2}]
    }
}
set s [Circle::new 5.0]
puts [Shape::area $s]   ;# dispatches to Circle::area -> 78.54

Note: a base.<name> method is automatically created for virtual methods, so the original implementation can be called by child classes when overriding it.

Visibility

Fields and methods in private { } blocks receive a my. prefix and are not exported.