LEARN VOID / DATA & BEHAVIOR
Generics
Reuse an operation while keeping its types.
func larger<t>(first: t, second: t) -> t:
if first > second:
return first
return second
func entry(score: i32) -> i32:
return larger(score, 10)Input 5Expected result 10
Name a reusable operation
larger<t> compares two values and returns the larger one. The type parameter t connects both parameters to the result type, so the returned value keeps the chosen concrete type.
Let the call choose the type
In larger(score, 10), the compiler infers the integer type from the arguments. Input 5 returns 10; input 15 returns 15. The same operation can be instantiated for another compatible numeric type.
Keep requirements visible
The function relies on > being valid for its concrete type. Generic code is still type-checked; this example does not make arbitrary objects orderable. Interfaces provide a way to express required methods when a generic operation needs specific behavior.