Currently, I am developing a transpiler for my own language, however, I find difficulties to transpile generic functions. For example, how would you translate the following Java-like code in to C?
interface Comparable<T> {
boolean compare(T that);
}
class Person implements Comprable<Person> {
public string name;
public boolean compare(Person that) {
return this.name.compareTo(that.name);
}
}
// The generic function
<T extends Comparable<T>> boolean isSorted(T[] xs) {
for(int i = 1; i < xs.length(); i++)
if(xs[i-1].compare(xs[i]) > 0)
return false
return true
}
My current solution is to use Javascript prototypes. So, the code above would be translated into the following Javascript (workable in Google Chrome):
class Person { }
Person.prototype.compare = function (that) {
return this.name.localeCompare(that.name);
}
function isSorted(xs) {
for (var i = 1; i < xs.length; i++)
if(xs[i-1].compare(xs[i]) > 0)
return false;
return true;
}
However, Javascript prototype suffers performance issue (I tested it myself), so I am looking for other ways to translate generics without using prototypes. That is why I'm looking for example on how to translate that into pure C.
Note: You might also translate it to any language you like, as long as you did not use generics.