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;
2
3// Generic container class
4class Box<T> {
5 private T item;
6
7 public Box(T item) {
8 this.item = item;
9 }
10
11 public fn get() -> T {
12 return this.item;
13 }
14}
15
16// Generic standalone function
17fn identity<T>(T val) -> T {
18 return val;
19}
20
21fn main() -> void {
22 Box<int> intBox = new Box<int>(42);
23 Box<string> strBox = new Box<string>("SP Language");
24
25 println("IntBox item: ${intBox.get()}");
26 println("StrBox item: ${strBox.get()}");
27
28 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.