+1

Build source code from Scala and Kotlin And Java

What i want to do this time has already been said as in the title, now i would like to jump right into the subject

First of all, in Scala's build tool sbt, you can compile not only the Scala program but also the Java program, but in addition to that you can also build Kotlin's program using kotlin-plugin 1 It will be able to do. This time, I tried to build Scala, Kotlin and Java program using this plugin.

It's simple to arrange so that it is inherited in order of ScalaClass extends JavaClass extends KotlinClass. I will paste the source code in order.

package com.github.kmizu.scala_kotlin_java

open class KotlinClass(open val name: String)

fun main(args: Array<String>) {
    println(KotlinClass("Kotlin").name)
}

I define a class called KotlinClass and output name in main. Next is the source of Java, and it looks like this:

package com.github.kmizu.scala_kotlin_java;

public class JavaClass extends KotlinClass {
    public JavaClass(String name) {
        super(name);
    }

    public static void main(String[] args) {
        System.out.println(new JavaClass("Java").getName());
    }
}

A method without getting defined which is defined as getName () is generated, but this is because Kotlin's properties appear as Java getters in JavaBeans format from Java.

Finally, here comes the Scala's source code:

package com.github.kmizu.scala_kotlin_java

object ScalaFile {
  class ScalaClass(val name: String) extends KotlinClass(name)

  def main(args: Array[String]): Unit = {
    println(new ScalaClass("Scala").name)
  }
}

Likewise, it inherits KotlinClass. Since ScalaClass 's constructor argument is defined with name val (same as Kotlin' s property), it can be referenced by name.

For each language's source code as following:

  • src/main/kotlin/com/github/kmizu/scala_kotlin_java
  • src/main/java/com/github/kmizu/scala_kotlin_java
  • src/main/scala/com/github/kmizu/scala_kotlin_java

And it follows the standard directory layout of each language. When I added the build definition (build.sbt) to IntelliJ IDEA, the complement of any language source code was working properly. It is wonderful.

Source code here is published, So please look into it if you are bored. By the way, sbt can build a project with mixed languages like this, but it can be used as a build tool for a single language. Well, it is mainly used for Scala, but I rarely see using sbt as a Java build tool.

There you go, hope to see you guys soon.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí