I've had an idea to use Transpiler classes that have methods to transpile supported types:
public class PythonTranspiler {
public String toPython(Pythonable pythonable) {
return pythonable.toPython();
}
public String toPython(Set<?> set) {
return String.format(
"set(%s)",
String.join(", ", set.stream().map(e -> this.toPython(e))));
}
}
public class JavaScriptTranspiler {
public String toJavaScript(JavaScriptable javaScriptable) {
return javaScriptable.toJavaScript();
}
public String toJavaScript(Set<?> set) {
return String.format(
"new Set([%s])",
String.join(", ", set.stream().map(e -> this.toJavaScript(e))));
}
}
But I'm not sure how to define a type for those that are translatable altogether (all the <?>s in the example below), and I guess things like map(e -> this.toJavaScript(e)) won't actually work as-written, because it will select the Set's generic type parameter method from JavaScriptTranspiler, not the actual object's type.