简体   繁体   中英

Generic function in Vala

I wrote a maximum() generic function in Vala. However, it does not compile. Here it is:

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

void main() {
    stdout.printf("%d\n", maximum(10, 2));
}

I got the following error:

generics.vala:2.12-2.16: error: Relational operation not supported for types `T' and `T'

Do you know how I can fix this function to be able to compile it? Thanks.

Generic direct comparison and various other operations aren't supported by current Vala. You may want to use and implement Gee.Comparable interface to use a compare_to() method instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM