Skip to main content
added 1113 characters in body
Source Link
user224123
user224123

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.

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.

Introductory statement changed, because it may have been misleading
Source Link
Doc Brown
  • 220.7k
  • 35
  • 410
  • 625

I want to allow some Java objects to be compiled totranslated into a string representation which matches Python or JavaScript stringsobjects.

I want to allow some Java objects to be compiled to Python or JavaScript strings.

I want to allow some Java objects to be translated into a string representation which matches Python or JavaScript objects.

added 253 characters in body
Source Link
user224123
user224123

And what if a user later wants to add a new compilation target? They make their own

public interface Swiftable {
    public String toSwift();
}

How can they add translations to all the existing objects that I've defined?

This doesn't seem very Java-supported.

This doesn't seem very Java-supported.

And what if a user later wants to add a new compilation target? They make their own

public interface Swiftable {
    public String toSwift();
}

How can they add translations to all the existing objects that I've defined?

This doesn't seem very Java-supported.

Source Link
user224123
user224123
Loading