Generics & Bounds
Parameterized functions, structs, enums, constrained generic classes, parameterized interfaces, monomorphization, and concrete generic errors in Wysna v0.7 / v0.8.
Wysna implements compile-time monomorphized generics across both Seed and the Self-Hosted Native SDK (native-core-v1). Generic definitions are specialized for each concrete type argument so runtime execution avoids boxing penalties and retains full static type checking.
1. Generic Functions
Section titled “1. Generic Functions”Declare type parameters inside square brackets [T, R] immediately after the function name:
fn identity[T](value: T) -> T { return value}
fn pair[A, B](first: A, second: B) -> (A, B) { return (first, second)}
fn main() -> unit { let x = identity[int](42) let p = pair[str, int]("port", 8080) print(f"x={x}, p=({p.0}, {p.1})")}2. Generic Structs & Recursive Enums
Section titled “2. Generic Structs & Recursive Enums”Product types (struct) and sum types (enum) accept type parameters, enabling strongly-typed wrappers, trees, and result-like structures:
struct Envelope[T] { request_id: str payload: T}
enum Tree[T] { Leaf(T) Branch(list[Tree[T]])}
fn main() -> unit { let env = Envelope[int] { request_id: "req-001", payload: 200, } print(f"{env.request_id}: {env.payload}")}3. Constrained Generics & Parameterized Interfaces
Section titled “3. Constrained Generics & Parameterized Interfaces”Attach interface bounds using [T: InterfaceName]. Since v0.8.0, both Seed and Selfhost support parameterized interfaces (interface Foo[T]), constrained generic classes, independent generic methods (transform[R: Show]), and parameterized inheritance:
interface Show { fn show() -> str}
interface Container[T: Show] { fn summary() -> str}
class Box[T: Show] implements Container[T] { value: T
fn init(value: T) -> unit { self.value = value }
pub fn summary() -> str { return f"Box[{self.value.show()}]" }
pub fn transform[R: Show](mapper: fn(T) -> R) -> R { return mapper(self.value) }}4. Concrete Generic Errors (LANG-07)
Section titled “4. Concrete Generic Errors (LANG-07)”Generic error types specialized to concrete arguments preserve distinct nominal error identity across T ! E channels and exhaustive handlers across all four engines:
struct ValidationError[T] { field: str rejected: T}
fn check_positive(n: int) -> int ! ValidationError[int] { if n <= 0 { return Err(ValidationError[int] { field: "count", rejected: n }) } return n}5. Compiler Boundaries & Checking Notes
Section titled “5. Compiler Boundaries & Checking Notes”- Uninstantiated templates: Effect reports (
wysna explain) and monomorphization operate on concrete instantiations. Instantiate generic functions or types in tests to verify all specialized paths. - Native profile diagnostics: Unresolved generic nominal values that exceed
native-core-v1report diagnostic[W2013]clearly rather than failing silently.