Generic Structs

Generic structs are quite close to generic classes. The following code uses a struct to define a type similar to a 2-tuple.

struct Pair<T, U> {
    let x: T
    let y: U
    public init(a: T, b: U) {
        x = a
        y = b
    }
    public func first(): T {
        return x
    }
    public func second(): U {
        return y
    }
}

main() {
    var a: Pair<String, Int64> = Pair<String, Int64>("hello", 0)
    println(a.first())
    println(a.second())
}

The program outputs the following.

hello
0

In Pair<T, U>, the first and second functions are provided to obtain the first and second elements of the "(T, U) tuple type-alike", so their return value types are respectively T and U type variables.