← Back to lessons

31 Generic Functions

Intermediate

Generic functions can by created by adding type parameter after the functions name.

To enforce certain type constraints you use where syntax. You write what classes or interfaces type substituted for T has to inherit/implement.

We can use this function on Int64, becuase it implements Comparable<Int64> interface.

We can also use the same function with Strings.

generic_functions.cj
func sort<T>(nums: Array<T>) where T <: Comparable<T> {
    for (i in 0..nums.size) {
        for (j in i + 1..nums.size) {
            if (nums[j] < nums[i]) {
                // This performs swap of two values:
                (nums[j], nums[i]) = (nums[i], nums[j])
            }
        }
    }
}

main() {
    var A = Array<Int64>([1, 2, 4, 1010, 69, 6, 69, 6, 6, 6, 345, 135, 4325,
                          243, 5432, 5423, 5, 4235, 4235])
    sort(A)
    for (i in A) {
        print("${i} ")
    }
    println()

    var B = Array<String>(["Hello", "my", "name", "is", "Gandalf!"])
    sort(B)
    for (i in B) {
        print("${i} ")
    }
    println()
}

// Output: 
// 1 2 4 5 6 6 6 6 69 69 135 243 345 1010 4235 4235 4325 5423 5432 
// Gandalf! Hello is my name