Vala中的通用功能

ant*_*oyo 6 generics function vala

我在Vala中编写了一个maximum()泛型函数.但是,它没有编译.这里是:

T maximum<T>(T a, T b) {
    return a > b ? a : b;
}

void main() {
    stdout.printf("%d\n", maximum(10, 2));
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

generics.vala:2.12-2.16: error: Relational operation not supported for types `T' and `T'
Run Code Online (Sandbox Code Playgroud)

你知道我怎么能修复这个函数才能编译它吗?谢谢.

elm*_*rco 5

当前的Vala不支持通用直接比较和各种其他操作.您可能希望使用并实现Gee.Comparable接口来改为使用compare_to()方法.