Zero-Cost Generics
SP implements generics using monomorphization. The compiler generates specialized, concrete LLVM machine code for each unique type instantiation (e.g., Box<int> and Box<string> become separate native symbols).
generics.sp
1import std.io;23// Generic container class4class Box<T> {5 private T item;67 public Box(T item) {8 this.item = item;9 }1011 public fn get() -> T {12 return this.item;13 }14}1516// Generic standalone function17fn identity<T>(T val) -> T {18 return val;19}2021fn main() -> void {22 Box<int> intBox = new Box<int>(42);23 Box<string> strBox = new Box<string>("SP Language");2425 println("IntBox item: ${intBox.get()}");26 println("StrBox item: ${strBox.get()}");2728 let echoed = identity<double>(3.14159);29 println("Echoed: ${echoed}");30}
No Runtime Overhead
Because generic code is fully monomorphized at compile time, access to generic members incurs zero runtime boxing, zero casting penalties, and zero virtual lookup indirection.