LEARN VOID / DATA & BEHAVIOR

Generics

Reuse typed operations with generics in VOID.

Complete program for the synchronous entry adapter. The result below is the returned integer.

generics.voidDownload source
func larger<t>(first: t, second: t) -> t:
    if second > first:
        return second
    return first

func entry(temperature: i32) -> i32:
    var morning_peak = larger(temperature, 12)
    return larger(morning_peak, 18)
Input 5Expected result 18

Connect inputs and result with a type parameter

larger<t> accepts two values of the same type and returns that type. Its comparison must be supported by the concrete argument type. Generics preserve static typing; they do not make arbitrary objects orderable.

Reuse the operation

The first call compares the input temperature with 12; the second compares that peak with 18. Input 5 returns 18, while input 25 returns 25. Each call still has just two arguments and one purpose.

Infer or state the type

Here t is inferred as i32. The first call can also be written larger<i32>(temperature, 12). Specializations are checked against their concrete operations. Interface constraints can express required method behavior; they do not bypass readonly or receiver-effect rules.