Object-Oriented Programming

SP supports full Object-Oriented Programming including reference classes with virtual tables, single inheritance, interface implementation contracts, and stack-allocated value structs.

oop.sp
1import std.io;
2
3// Interface contract
4interface Greeter {
5 fn greet() -> void;
6}
7
8// Base class with virtual method
9class Person : Greeter {
10 protected string name;
11
12 public Person(string name) {
13 this.name = name;
14 }
15
16 public virtual fn greet() -> void {
17 println("Hello, I am ${this.name}");
18 }
19}
20
21// Single inheritance & method override
22class Employee : Person {
23 private string role;
24
25 public Employee(string name, string role) : super(name) {
26 this.role = role;
27 }
28
29 public override fn greet() -> void {
30 println("Greetings, I am ${this.name}, working as ${this.role}");
31 }
32}
33
34// Lightweight value-type struct (stack allocated)
35struct Point {
36 int x;
37 int y;
38}
39
40fn main() -> void {
41 Person p = new Employee("Sarah", "Systems Engineer");
42 p.greet();
43
44 Point pt = Point(10, 20);
45 println("Point: (${pt.x}, ${pt.y})");
46}

Classes vs Structs

Class (Reference Type)

Allocated on heap with new. Supports single inheritance, vtable virtual dispatch, polymorphism, and access modifiers.

Struct (Value Type)

Stack-allocated with zero heap allocator overhead. Passed by value and ideal for coordinate pairs, vector math, and compact records.