← Back to Docs

Objects (new)

Use new to create object instances from classes. Objects have their own fields and methods.

Creating objects

Define a class as a blueprint, then instantiate it with new ClassName():

class Point {
    let x: int = 0;
    let y: int = 0;
}

class Main {
    let p = new Point();
    print(p);           // <Point object>
}

When new is called, VDX runs the class body to initialize fields (via let) and registers methods (via fn).

Accessing fields

Read fields with dot notation:

let p = new Point();
print(p.x);    // 0
print(p.y);    // 0

Setting fields

Assign to fields with dot notation:

let p = new Point();
p.x = 10;
p.y = 20;
print(p.x, p.y);   // 10 20

Methods

Functions declared in the class become methods on the object. Methods can read and modify the object's fields:

class Point {
    let x: int = 0;
    let y: int = 0;

    fn setXY(nx, ny) {
        x = nx;
        y = ny;
    }

    fn describe() {
        print("Point:", x, y);
    }
}

class Main {
    let p = new Point();
    p.setXY(5, 15);
    p.describe();       // Point: 5 15
}

Methods with return

class Counter {
    let count: int = 0;

    fn increment() {
        count = count + 1;
    }

    fn getCount() {
        return count;
    }
}

class Main {
    let c = new Counter();
    c.increment();
    c.increment();
    print(c.getCount());   // 2
}

Multiple instances

Each new call creates an independent object with its own fields:

class Main {
    let a = new Point();
    let b = new Point();
    a.x = 1;
    b.x = 99;
    print(a.x);    // 1
    print(b.x);    // 99
}

Objects are truthy

Objects are always truthy in boolean context:

let p = new Point();
if (p) {
    print("object exists");
}

Top-level vs blueprint

Classes serve a dual role in VDX:

  • As a top-level entry point — the class body runs immediately when the file is executed
  • As a blueprint — used with new to create object instances

A class can be both. The top-level class (like Main) runs its body, while blueprint classes (like Point) are used via new.