Skip to main content

Generics & Interfaces

RGC supports generic functions, generic classes, and generic enums, with interfaces providing static dispatch and bounds.

Generic functions​

def first[T](items: @List[T]) -> T {
return items[0]
}

Generic classes​

class Pair[A, B] {
first: A
second: B
}

Generic enums​

enum Option[T] {
Some(value: T)
None
}

Interfaces​

Interfaces define behavioral contracts; types implement them:

interface Show {
def to_string(self) -> str
}

class Point {
x: int
y: int
}

implement Show for Point {
def to_string(self) -> str {
return "({}, {})".format(self.x, self.y)
}
}

Bounds​

Generic parameters can be constrained by interfaces:

def print_all[T: Show](items: @List[T]) {
for item in items {
print(item.to_string())
}
}

Monomorphization​

Generics are compiled via monomorphization: each concrete type gets specialized code — no vtable overhead, no boxing, with performance identical to hand-written specialized implementations.

Continue reading​