Skip to content

Classes and Inheritance

Frank edited this page Jan 27, 2026 · 3 revisions

Classes & Inheritance

Table of Contents

Table of Contents

CupidScript supports classes with constructors, methods, and single inheritance.

Defining Classes

class File {
  fn new(path) {
    self.path = path;
  }

  fn is_hidden() {
    return starts_with(self.path, ".");
  }
}
  • fn new(...) is the constructor.
  • Methods are declared with fn inside the class body.
  • self refers to the instance inside methods.

Creating Instances

Classes are callable. Calling a class creates an instance and runs new if present:

let f = File(".secret");
print(f.is_hidden());

Instance fields are dynamic and can be assigned at runtime:

f.owner = "frank";
print(f.owner);

Inheritance

Use : Parent to inherit methods and reuse constructors:

class ImageFile : File {
  fn new(path) {
    super.new(path);
    self.is_img = ends_with(path, ".png");
  }

  fn is_image() {
    return self.is_img;
  }
}
  • super refers to the parent class inside methods.
  • super.new(...) is typically used in child constructors.

Method Calls

let img = ImageFile("cat.png");
print(img.is_image());
print(img.is_hidden()); // inherited

Clone this wiki locally