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;23// Interface contract4interface Greeter {5 fn greet() -> void;6}78// Base class with virtual method9class Person : Greeter {10 protected string name;1112 public Person(string name) {13 this.name = name;14 }1516 public virtual fn greet() -> void {17 println("Hello, I am ${this.name}");18 }19}2021// Single inheritance & method override22class Employee : Person {23 private string role;2425 public Employee(string name, string role) : super(name) {26 this.role = role;27 }2829 public override fn greet() -> void {30 println("Greetings, I am ${this.name}, working as ${this.role}");31 }32}3334// Lightweight value-type struct (stack allocated)35struct Point {36 int x;37 int y;38}3940fn main() -> void {41 Person p = new Employee("Sarah", "Systems Engineer");42 p.greet();4344 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.