Generic Structs
Generic struct types are similar to classes. Below is an example of using a struct to define a binary tuple-like type:
The program output is:
The Pair struct provides two functions first and second to retrieve the first and second elements of the tuple respectively.
cangjie
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())
}text
hello
0