Skip to content

Traits, Extensions & Modules

Every .splx file is one ES Module. Declarations are private by default and exported with pub:

lib/math.splx
pub fn square(n: Int) -> Int {
n * n
}

Import by namespace, named destructuring, alias, or both:

import fs from "std/fs"
import { square as sq } from "./lib/math.splx"
import llm, { Approve, Reject } from "std/llm"

Attach methods to any type — including structural open record shapes — scoped by module import:

// Works on any record that has `firstName: Str` and `lastName: Str`
pub fn { firstName: Str, lastName: Str, .. }.full_name(self) -> Str {
self.firstName + " " + self.lastName
}
// Mutating extension method on a generic type
pub fn Stack<T>.push(mut self, item: T) {
self.elements.push(item)
self.length = self.length + 1
}

Declare traits with trait and implement them with impl:

trait Describable {
fn describe(self) -> Str
}
type Point = struct { x: Int, y: Int }
impl Describable for Point {
fn describe(self) -> Str {
"(${self.x}, ${self.y})"
}
}
fn log_item<T: Describable>(item: T) {
print(item.describe())
}

Because SPL records are structurally typed, two impl blocks for the same trait on structurally identical record shapes in the same scope are rejected at compile time. When you need distinct trait behavior for the same underlying shape, wrap it in a nominal single-variant enum (newtype):

enum Celsius { Celsius(Float) }
enum Fahrenheit { Fahrenheit(Float) }