Language
Generics
Tea uses square-bracket type parameters for reusable functions and structs.
Generics let you write one function or struct that works across multiple concrete types. Instead of copying the same shape for Int, String, and other values, you parameterize the definition once and reuse it.
Generic structs and functions
Tea uses square brackets for type parameters. You will see them on both struct definitions such as Box[T] and function definitions such as def identity[T](...).
struct Box[T] {
value: T
}
def identity[T](value: T) -> T
return value
end
const a = identity[Int](42)
const b = identity[String]("tea")
const boxed = Box[String](value: "leaf")
@println(a)
@println(b)
@println(boxed.value)In day-to-day code, generics are most useful when the logic stays the same but the concrete type changes. That applies to wrappers, containers, and small reusable helpers.
What to notice
- Generic parameters use
Box[T]anddef identity[T](...)syntax. Explicit instantiation like
identity[Int](42)is supported in current examples.- The same definition can be reused across multiple concrete types.
- The compiler monomorphizes concrete uses for code generation.