3

I'd like to use Kotlin & Scala together in projects, and maybe some other languages, but I've seen no good way of doing it. The only way I thought of was compiling one language and decompiling it into Java to work with the other. Are there any alternatives?

jado
  • 922
  • 9
  • 21
  • 1
    A more specific question might get you a more specific answer, like "I have a class defined in Scala. How can I create an instance of it from Kotlin?" – user253751 Jun 09 '15 at 12:41
  • Could you use Gradle, and use the Kotlin and Scala plugins? – Seer Jun 13 '15 at 12:46
  • This really depends on whether you are in an IDE, and which IDE. And whether you are in a build tool, and which build tool. Your question needs more clarification. Also add an example of your current code layout, what directory structure you are using, are they in same or separate modules (sub projects), etc. – Jayson Minard Jan 05 '16 at 12:04
  • Also note what you have tried before that didn't work. – Jayson Minard Jan 05 '16 at 12:05

2 Answers2

4

For the sake of completeness and not putting words into someone else's mouth, I wanted to weigh in.

I agree with the last sentence of ziggystar's answer. The right thing to do is to take a component-based approach and not try to combine multiple languages in one component or project.

From a technical perspective, each of the JVM languages has their own compiler. Some, such as Scala's, can compile both Scala and Java files. However, this may or may not be true for other compilers. In order to avoid strange build processes, a good approach would be to use a single language for every built module.

Since you're sticking to JVM languages, every languages can be compiled into a JAR, so you can easily distribute your executable binary as a single JAR file, with all of the components wrapped up inside it. This is the Fat JAR approach (see this question on Stack Overflow, this post on Java Code Geeks).

From a human readability perspective, this should also make your software more easily understood. Not only have you decomposed it into logical building blocks (each component), but someone making modifications only needs to understand the language that the component they are working on is written in and the public interface of the components they need to interact with. There's no mental context switching between languages.

Community
  • 1
  • 1
Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
3

You can use Scala and Java simultaneously, since scalac understands and compiles Java files. The same probably holds for other languages. Problems might arise when using multiple alternative JVM languages, since, e.g., the Kotlin compiler probably can't understand the Scala files and vice versa.

I think the best way would be to split the project into different modules, and use at most one alternative language per module.

What do I mean with module?

With module, I mean a set of source files that gets translated into one (binary) artifact, i.e. a jar file. Under different circumstances I would simply call a "module" a project. Note that a module may depend on other modules on the binary level (e.g. has some jar files as dependencies).

Multi module support in IDEs

I think it should be possible with most major IDEs to work on different modules simultaneously, even if each module uses a different language. Terminology varies across IDEs.

Terminology

For Intellij IDEA, one of my modules is called "module". For Eclipse it would be called "project".

Community
  • 1
  • 1
ziggystar
  • 28,410
  • 9
  • 72
  • 124
  • I want to use multiple alternative languages in the same project. They're all getting translated into bytecode, so there must be a way to use them together before that. – jado Jun 09 '15 at 12:40
  • 1
    @Phase What do you mean by "one project"? Do you mean one deliverable thing? One root level in source code? Or one IDE level project? – Thomas Owens Jun 09 '15 at 12:42
  • Also, what does @ziggystar mean by a "module"? (See Thomas Owens' comment) – user253751 Jun 09 '15 at 12:43
  • @ThomasOwens I'd think an IDE level project, but I see no way of doing this in an IDE. I'd like to get one jar out of the different types of files (.java, .scala, and .kt). – jado Jun 09 '15 at 12:44
  • I believe the last sentence of this answer is the correct thing, then. Java, Scala, and Kotlin (and other JVM languages) all compile into JARs. Have separate IDE projects for each module and build each one into a JAR with well-defined interfaces. At the end, you need a main entry point, so you can package all of your component JARs in different languages into one JAR at the end. – Thomas Owens Jun 09 '15 at 12:46